Let’s say I have the following scenario of class not marked with UCLASS() and a property not marked with UPROPERTY():
class Example
{
Example()
{
MyObj = NewObject<UObject>();
}
~Example()
{
// Do I need to do anything here or will MyObj be cleaned up automatically?
}
UObject* MyObj;
}
Is there anything I need to put in the destructor to ensure that the UObject is cleaned up properly?
So if you don’t set any outer pointer to the object at creation, the pointer doesn’t belong to any world and the engine is not aware of your property because you don’t use the macros.
It’s seem to me that is classic c++ with some unreal engine definitions, so just call what i said earlier without the GC call after the function.
It should be ok as classic c++ is when you manage yourself pointer and others stuff.
Anyways the gc will be call by unreal itself to cleanup.
So just check out where you use that pointer if it’s not null.
Is this code sufficient?
I mean ConditionalBeginDestroy seems to have IsValidLowLevel check, and objectToDestroy == nullptr is in IsValidLowLevel()
Do I really need to write FileRepo = nullptr; after FileRepo->ConditionalBeginDestroy();
I tried not to use pointers but the code below throws an exception
Note that the whole point of UObject-based classes is garbage collection so you don’t have to free it manually.
Also, don’t keep an UObject-based pointer as a class member as the question mention. UObjects are guaranteed to be valid and not garbage collected only if they are referenced somewhere. This is fine if your code runs on main thread and you don’t keep the reference across ticks. Also, accessing a garbage collected object not marked as a UPROPERTY() is hard to debug as your pointer won’t be nullptr but point to freed memory.