How to properly handle EXCEPTION_ACCESS_VIOLATION

I have been struggling with a transient bug that I can’t reliably replicate, EXCEPTION_ACCESS_VIOLATION. I have the following code.

.h

UFUNCTION(Reliable, NetMulticast)
virtual void StopSound();

.cpp

void AXLItem::StopSound_Implementation()
{
	if (GetNetMode() != NM_DedicatedServer)
	{
		if (AudioComponent)
		{
			AudioComponent->FadeOut(0.1f, 0.0f);
		}
	}
}

Ocassionaly this method with throw the following error, the line numbers aren’t matching up here but the line that throws the error is AudioComponent->FadeOut(0.1f, 0.0f);:

Fatal error!

Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0xe3190b58

0x00007ff78f2df472 IO-Win64-Shipping.exe!AXLItem::StopSound_Implementation() [c:\users\jeffd\documents\github\io\plugins\extendedlibrary\source\extendedlibrary\private\items\xlitem.cpp:125]
0x00007ff78f657a12 IO-Win64-Shipping.exe!UFunction::Invoke() [c:\users\jeffd\documents\github\unrealengine\engine\source\runtime\coreuobject\private\uobject\class.cpp:4862]
0x00007ff78f76f680 IO-Win64-Shipping.exe!UObject::ProcessEvent() [c:\users\jeffd\documents\github\unrealengine\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:1480]
0x00007ff7901da9a4 IO-Win64-Shipping.exe!AActor::ProcessEvent() [c:\users\jeffd\documents\github\unrealengine\engine\source\runtime\engine\private\actor.cpp:719]
0x00007ff78f2ceae7 IO-Win64-Shipping.exe!UXLSemiautoBurstFireComponent::DetermineAction() [c:\users\jeffd\documents\github\io\plugins\extendedlibrary\source\extendedlibrary\private\weapons\component\xlsemiautoburstfirecomponent.cpp:30]
0x00007ff78f657a12 IO-Win64-Shipping.exe!UFunction::Invoke() [c:\users\jeffd\documents\github\unrealengine\engine\source\runtime\coreuobject\private\uobject\class.cpp:4862]
0x00007ff78f76f680 IO-Win64-Shipping.exe!UObject::ProcessEvent() [c:\users\jeffd\documents\github\unrealengine\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:1480]
0x00007ff78f2c50d8 IO-Win64-Shipping.exe!TMulticastScriptDelegate<FWeakObjectPtr>::ProcessMulticastDelegate<UObject>() [c:\users\jeffd\documents\github\unrealengine\engine\source\runtime\core\public\uobject\scriptdelegates.h:488]
0x00007ff79079b13e IO-Win64-Shipping.exe!FInputActionUnifiedDelegate::Execute() [c:\users\jeffd\documents\github\unrealengine\engine\source\runtime\engine\classes\components\inputcomponent.h:250]
0x00007ff7907a9f53 IO-Win64-Shipping.exe!UPlayerInput::ProcessInputStack() [c:\users\jeffd\documents\github\unrealengine\engine\source\runtime\engine\private\userinterface\playerinput.cpp:1298]
0x00007ff790619aa9 IO-Win64-Shipping.exe!APlayerController::ProcessPlayerInput() [c:\users\jeffd\documents\github\unrealengine\engine\source\runtime\engine\private\playercontroller.cpp:2479]
0x00007ff790621967 IO-Win64-Shipping.exe!APlayerController::TickPlayerInput() [c:\users\jeffd\documents\github\unrealengine\engine\source\runtime\engine\private\playercontroller.cpp:4404]
0x00007ff790618474 IO-Win64-Shipping.exe!APlayerController::PlayerTick() [c:\users\jeffd\documents\github\unrealengine\engine\source\runtime\engine\private\playercontroller.cpp:2137]
0x00007ff790620fde IO-Win64-Shipping.exe!APlayerController::TickActor() [c:\users\jeffd\documents\github\unrealengine\engine\source\runtime\engine\private\playercontroller.cpp:4543]
0x00007ff7901cbe75 IO-Win64-Shipping.exe!FActorTickFunction::ExecuteTick() [c:\users\jeffd\documents\github\unrealengine\engine\source\runtime\engine\private\actor.cpp:140]
0x00007ff79076709a IO-Win64-Shipping.exe!TGraphTask<FTickFunctionTask>::ExecuteTask() [c:\users\jeffd\documents\github\unrealengine\engine\source\runtime\core\public\async\taskgraphinterfaces.h:829]
0x00007ff78f4eaafa IO-Win64-Shipping.exe!FNamedTaskThread::ProcessTasksNamedThread() [c:\users\jeffd\documents\github\unrealengine\engine\source\runtime\core\private\async\taskgraph.cpp:678]
0x00007ff78f4eac03 IO-Win64-Shipping.exe!FNamedTaskThread::ProcessTasksUntilQuit() [c:\users\jeffd\documents\github\unrealengine\engine\source\runtime\core\private\async\taskgraph.cpp:575]
0x00007ff78f4f1e89 IO-Win64-Shipping.exe!FTaskGraphImplementation::WaitUntilTasksComplete() [c:\users\jeffd\documents\github\unrealengine\engine\source\runtime\core\private\async\taskgraph.cpp:1444]
0x00007ff79077a68f IO-Win64-Shipping.exe!FTickTaskSequencer::ReleaseTickGroup() [c:\users\jeffd\documents\github\unrealengine\engine\source\runtime\engine\private\ticktaskmanager.cpp:556]
0x00007ff79077e537 IO-Win64-Shipping.exe!FTickTaskManager::RunTickGroup() [c:\users\jeffd\documents\github\unrealengine\engine\source\runtime\engine\private\ticktaskmanager.cpp:1474]
0x00007ff790499a37 IO-Win64-Shipping.exe!UWorld::Tick() [c:\users\jeffd\documents\github\unrealengine\engine\source\runtime\engine\private\leveltick.cpp:1460]
0x00007ff7903e62c7 IO-Win64-Shipping.exe!UGameEngine::Tick() [c:\users\jeffd\documents\github\unrealengine\engine\source\runtime\engine\private\gameengine.cpp:1367]
0x00007ff78f2b6059 IO-Win64-Shipping.exe!FEngineLoop::Tick() [c:\users\jeffd\documents\github\unrealengine\engine\source\runtime\launch\private\launchengineloop.cpp:3699]
0x00007ff78f2bba80 IO-Win64-Shipping.exe!GuardedMain() [c:\users\jeffd\documents\github\unrealengine\engine\source\runtime\launch\private\launch.cpp:174]
0x00007ff78f2bbb0a IO-Win64-Shipping.exe!GuardedMainWrapper() [c:\users\jeffd\documents\github\unrealengine\engine\source\runtime\launch\private\windows\launchwindows.cpp:145]
0x00007ff78f2c4237 IO-Win64-Shipping.exe!WinMain() [c:\users\jeffd\documents\github\unrealengine\engine\source\runtime\launch\private\windows\launchwindows.cpp:276]
0x00007ff790ea3d6a IO-Win64-Shipping.exe!__scrt_common_main_seh() [d:\agent\_work\2\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288]
0x00007ffb2e9e7bd4 KERNEL32.DLL!UnknownFunction []
0x00007ffb2f58ced1 ntdll.dll!UnknownFunction []

Usually when I get this error it means a null reference exception but I am performing a null check. If anyone has any insight I’d greatly appreciate it.

Is your AudioComponent a UPROPERTY? If it isn’t, the component could be getting garbage collected.

When it gets garbage collected, the pointer stored in your AXLItem could still reference where the component was even though the memory has been deallocated, meaning that AudioComponent will not be null but reference invalid memory.