Download

Memory (de)allocation and garbage collection

How is this all handled? What is the proper way to allocate a garbage collected object? The correct way to force that object to deallocate? How do you allocate a non-garbage collected object?

Dear MMavipc,

I will go write a Wiki on this topic, but to get you started

"How do you allocate a non-garbage collected object?"

In the .h make sure there is at least 1 reference to the new object that is a UPROPERTY()

you could even have all your references (to ensure non-garbage collection) in a TArray



UPROPERTY()
TArray<UObject*> GCGoAway;


"What is the proper way to allocate a garbage collected object?"



//in any class
UStaticMesh* DynamicSM = NewObject<UStaticMesh>(this);


this = Outer, if looking through UObject classes.

"The correct way to force that object to deallocate?"



if(!MyObject) return;
if(!MyObject->IsValidLowLevel()) return;

MyObject->BeginDestroy();
MyObject = nullptr;


The above might be open to plenty of debate but this is how I’ve been doing it for months for my In-Game Editor where I create and delete stuff all the time.

IsValidLowLevel() is a very necessary safety check to make sure the Object is not already in the process of being deleted.

If you dont do this check, ALLLL of the time, you will crash to desktop a lot

:slight_smile:

So I wrote a macro for it cause it is so important :slight_smile:


**Gift for You**

Judging from your questions you'll probably be interested in my [Dynamic Load Object wiki tutorial](https://wiki.unrealengine.com/Dynamic_Load_Object),

where you can load a runtime reference to what is one of the .uassets of your project.

So you can easily load a UStaticMesh for example, that you've created in the UE4 Editor.

**[Dynamic Load Object](https://wiki.unrealengine.com/Dynamic_Load_Object)**


Rama

PS: thanks for your own awesome contributions to this new UE4 community!

That seems like a hacky way to do it, instead of disabling the refcounting, it’s just always keeping a reference to it.

Objects should report references to other objects so that the garbage collector can find them. If you don’t report references the object may be garbage collected at any point. Using the UPROPERTY macro on an object variable will allow the object system to automatically find references. If you simply null out the pointer to the object on your class when you are done with it, that is sufficient enough for the garbage collector to destruct the object (provided there are no other references to it). You can give a much more forceful hint to the garbage collector by calling Object->MarkPendingKill() although this is not generally needed.

Also you should not call BeginDestroy manually. That is just a function called on an object when it is being destroyed by the garbage collector to allow any additional cleanup. Calling it manually does not delete the object and it would just call code that may not do anything and gives the object incorrect behavior since it isn’t being destroyed. Many objects do nothing in their BeginDestroy functions.

For c++ classes that aren’t based off of UObject but still contain object references, those classes should implement the FSerializableObject interface to report references.

If you know that the lifetime of an object is managed elsewhere and you just want to observe it and not contribute any references to it, you can hold onto a TWeakObjectPtr to that object which will safely be nulled out when the object is destroyed.
E.g., TWeakObjectPtr<UStaticMesh> MeshThatCouldBeNull.

In all my conversations with Epic Devs via the UDN this is what they recommend,

additionally Epic Dev Matt has graced you with a reply directly addressing this: