UMG added child to scrollbox gets GC'd during execution.

Hi.

Remember Rama’s Tutorial on creating dynamic lists of widgets (A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine Forums) ? Well for some reason, if I leave my UMG widget on the screen too long, the elements populating the scroll box will get garbage collected and crash the game.

[2017.11.25-05.45.42:968][250]LogUObjectBase: Error: Virtual functions table is invalid.
Fatal error: [File:C:\Users\Moi\MyEngines\UnrealEngine\Engine\Source\Runtime\CoreUObject\Private\UObject\GarbageCollection.cpp] [Line: 554] 
Invalid object in GC: 0x0000022800000000, ReferencingObject: NPCDialogueElement_C /Engine/Transient.UnrealEdEngine_0:GalileenGameInstance_0.NPCDialogueElement_C_0, ReferencingObjectClass: WidgetBlueprintGeneratedClass /Game/UMG/NPCDialogue/NPCDialogueElement.NPCDialogueElement_C, Property Name: Icone, Offset: 1360, TokenIndex: 27
UE4Editor.exe has triggered a breakpoint.

The NPCDialogueElement is the UMG widget I use to create entries into a scroll box on some master widget. It was created using the UMG editor.

I tried saving the created widgets in an array, even used a UPROPERTY() decorated array inside the C++ parent of this blueprint but it keeps on crashing with the same error.

The callstack reveals little to me.

UE4Editor-CoreUObject.dll!TFastReferenceCollector<1,FGCReferenceProcessor<1>,FGCCollector<1>,FGCArrayPool,0>::ProcessObjectArray(FGCArrayStruct & InObjectsToSerializeStruct, const TRefCountPtr<FGraphEvent> & MyCompletionGraphEvent) Line 614	C++
 	UE4Editor-CoreUObject.dll!TFastReferenceCollector<1,FGCReferenceProcessor<1>,FGCCollector<1>,FGCArrayPool,0>::FCollectorTaskQueue::DoTask() Line 299	C++
 	UE4Editor-CoreUObject.dll!TGraphTask<TFastReferenceCollector<1,FGCReferenceProcessor<1>,FGCCollector<1>,FGCArrayPool,0>::FCollectorTaskProcessorTask>::ExecuteTask(TArray<FBaseGraphTask *,FDefaultAllocator> & NewTasks, ENamedThreads::Type CurrentThread) Line 784	C++
 	UE4Editor-Core.dll!FTaskThreadAnyThread::ProcessTasks() Line 907	C++
 	UE4Editor-Core.dll!FTaskThreadAnyThread::ProcessTasksUntilQuit(int QueueIndex) Line 785	C++
 	UE4Editor-Core.dll!FTaskThreadBase::Run() Line 501	C++
 	UE4Editor-Core.dll!FRunnableThreadWin::Run() Line 76	C++
 	UE4Editor-Core.dll!FRunnableThreadWin::GuardedRun() Line 25	C++
 	[External Code]	

Any hint appreciated.

Whoah. Turns out I need to read everything from the error message :slight_smile:

The culprit is the property name “Icone”. Icone is not part of NPCDialogueElement but from a UStruct that I pass to the class for data and callback purposes. It has a pointer to a UTexture2D object. that is sometimes used, other times it isn’t. And when not used, it’s uninitialized, probably to a value that is not 0. The GC spots it and tries to free it it, I guess.

Moral of the story: Don’t let dangling pointers uninitialized, children. A struct (or USTRUCT) can be easily initialized like a class constructor using:

USTRUCT(BlueprintType)
struct MyStruct()
{
 public:

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Dialogs)
UTexture2D *Icone;

   (...)

    MyStruct() : Icone(nullptr) { }

};

Hope this will be useful to somebody.