Bug? Access Violation when accessing Widget Pointer

Hi there

I have some problems with an “Access Violation” crash when trying to access a pointer to a widget.

Should I not use pointers to Widgets?

I have read some articles about the same problem when trying to use
MouseOverWidgetRef->IsValidLowLevel(); and that we should first check if the object isn’t a null pointer, what I did.

Could this happen because the function gets called twice in one frame?

Any advice would be very helpfull. Thank you very much!


Error in Line 224:

if (MouseOverWidgetRef != nullptr) {)

**InteractiveItem.h**
class UItemMouseOverWidget* MouseOverWidgetRef = nullptr;

**InteractiveItem.cpp**
if (MouseOverWidgetRef != nullptr) {
	if (MouseOverWidgetRef->IsValidLowLevel()) {
		MouseOverWidgetRef->RemoveFromParent();
		MouseOverWidgetRef = nullptr;
	}
}

alternative InteractiveItem.cpp

if (MouseOverWidgetRef) {
	if (MouseOverWidgetRef->IsValidLowLevel()) {
		MouseOverWidgetRef->RemoveFromParent();
		MouseOverWidgetRef = nullptr;
	}
}

**Log:**
Access violation - code c0000005 (first/second chance not available)

UE4Editor_Survivor!AInteractiveItem::ToggleMouseOverWidget() [d:\eigene dateien\programming\unreal 4 engine\survivor\trunk\source\survivor\private\interactiveitem.cpp:224]
UE4Editor_Survivor!UCraftingSpaceComponent::UpdateCraftingSpace() [d:\eigene dateien\programming\unreal 4 engine\survivor\trunk\source\survivor\private\craftingspacecomponent.cpp:636]
UE4Editor_Survivor!ARealInventory::UpdateInventory() [d:\eigene dateien\programming\unreal 4 engine\survivor\trunk\source\survivor\private\realinventory.cpp:311]
UE4Editor_Survivor!ARealInventory::execUpdateInventory() [d:\eigene dateien\programming\unreal 4 engine\survivor\trunk\source\survivor\public\realinventory.h:26]
UE4Editor_CoreUObject!UFunction::Invoke() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\coreuobject\private\uobject\class.cpp:4542]
UE4Editor_CoreUObject!UObject::CallFunction() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:732]
UE4Editor_CoreUObject!UObject::ProcessContextOpcode() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:2167]
UE4Editor_CoreUObject!UObject::ProcessInternal() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:954]
UE4Editor_CoreUObject!UObject::CallFunction() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:856]
UE4Editor_CoreUObject!UObject::ProcessInternal() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:954]
UE4Editor_CoreUObject!UObject::CallFunction() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:856]
UE4Editor_CoreUObject!UObject::ProcessInternal() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:954]
UE4Editor_CoreUObject!UObject::CallFunction() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:856]
UE4Editor_CoreUObject!UObject::ProcessInternal() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:954]
UE4Editor_CoreUObject!UObject::CallFunction() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:856]
UE4Editor_CoreUObject!UObject::ProcessInternal() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:954]
UE4Editor_CoreUObject!UObject::CallFunction() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:856]
UE4Editor_CoreUObject!UObject::ProcessInternal() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:954]
UE4Editor_CoreUObject!UObject::CallFunction() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:856]
UE4Editor_CoreUObject!UObject::ProcessContextOpcode() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:2167]
UE4Editor_CoreUObject!UObject::ProcessInternal() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:954]
UE4Editor_CoreUObject!UObject::CallFunction() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:856]
UE4Editor_CoreUObject!UObject::ProcessInternal() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:954]
UE4Editor_CoreUObject!UObject::CallFunction() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:856]
UE4Editor_CoreUObject!UObject::ProcessInternal() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:954]
UE4Editor_CoreUObject!UObject::CallFunction() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:856]
UE4Editor_CoreUObject!UObject::ProcessInternal() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:954]
UE4Editor_CoreUObject!UObject::CallFunction() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:856]
UE4Editor_CoreUObject!UObject::ProcessContextOpcode() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:2167]
UE4Editor_CoreUObject!UObject::ProcessInternal() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:954]
UE4Editor_CoreUObject!UObject::CallFunction() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:856]
UE4Editor_CoreUObject!UObject::ProcessInternal() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:954]
UE4Editor_CoreUObject!UFunction::Invoke() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\coreuobject\private\uobject\class.cpp:4542]
UE4Editor_CoreUObject!UObject::ProcessEvent() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:1314]
UE4Editor_Engine!AActor::ProcessEvent() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\engine\private\actor.cpp:693]
UE4Editor_Engine!FInputActionHandlerDynamicSignature::Execute() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\engine\classes\components\inputcomponent.h:108]
UE4Editor_Engine!FInputActionUnifiedDelegate::Execute() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\engine\classes\components\inputcomponent.h:212]
UE4Editor_Engine!UPlayerInput::ProcessInputStack() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\engine\private\userinterface\playerinput.cpp:1225]
UE4Editor_Engine!APlayerController::ProcessPlayerInput() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\engine\private\playercontroller.cpp:2478]
UE4Editor_Engine!APlayerController::TickPlayerInput() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\engine\private\playercontroller.cpp:4229]
UE4Editor_Engine!APlayerController::PlayerTick() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\engine\private\playercontroller.cpp:2142]
UE4Editor_Engine!APlayerController::TickActor() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\engine\private\playercontroller.cpp:4323]
UE4Editor_Engine!FActorTickFunction::ExecuteTick() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\engine\private\actor.cpp:132]
UE4Editor_Engine!FTickFunctionTask::DoTask() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\engine\private\ticktaskmanager.cpp:273]
UE4Editor_Engine!TGraphTask<FTickFunctionTask>::ExecuteTask() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\core\public\async\taskgraphinterfaces.h:784]
UE4Editor_Core!FNamedTaskThread::ProcessTasksNamedThread() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\core\private\async\taskgraph.cpp:651]
UE4Editor_Core!FNamedTaskThread::ProcessTasksUntilQuit() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\core\private\async\taskgraph.cpp:560]
UE4Editor_Core!FTaskGraphImplementation::WaitUntilTasksComplete() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\core\private\async\taskgraph.cpp:1330]
UE4Editor_Engine!FTickTaskSequencer::ReleaseTickGroup() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\engine\private\ticktaskmanager.cpp:542]
UE4Editor_Engine!FTickTaskManager::RunTickGroup() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\engine\private\ticktaskmanager.cpp:1454]
UE4Editor_Engine!UWorld::RunTickGroup() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\engine\private\leveltick.cpp:760]
UE4Editor_Engine!UWorld::Tick() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\engine\private\leveltick.cpp:1408]
UE4Editor_UnrealEd!UEditorEngine::Tick() [d:\build\++ue4+release-4.18+compile\sync\engine\source\editor\unrealed\private\editorengine.cpp:1659]
UE4Editor_UnrealEd!UUnrealEdEngine::Tick() [d:\build\++ue4+release-4.18+compile\sync\engine\source\editor\unrealed\private\unrealedengine.cpp:396]
UE4Editor!FEngineLoop::Tick() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\launch\private\launchengineloop.cpp:3296]
UE4Editor!GuardedMain() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\launch\private\launch.cpp:166]
UE4Editor!GuardedMainWrapper() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\launch\private\windows\launchwindows.cpp:134]
UE4Editor!WinMain() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\launch\private\windows\launchwindows.cpp:210]
UE4Editor!__scrt_common_main_seh() [f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl:253]
kernel32
ntdll

Turns out that the access violation was a false error message. After recreating project files and compiling it again, I found a better error message in the log file. Problem was, that an array changed while I was iterating trough it. I made a copy of said array which fixed the “bug”/error. Its just strange that the pointer to the element in the for each loop was kind of valid but somehow not the objects inside the element.