Download

Avoiding UE4 Aggressive GC

So I’ve been having a hell of a time with Unreal’s garbage collection over the last couple of days. I should preface by saying that I have some objects which run on a separate thread and aren’t UObjects which I’m able to perform manual memory management on just fine, but for some reason when I use new in a UClass the garbage collector aggressively tracks the object, and it appears to delete anything that I create on the heap as soon as it goes out of scope, this would normally be fine but I’m creating different type variables and assigning them to void pointers so I feel it’s likely that there is probably some memory leak occuring here. I’ve tried using TSharedPtr and custom deleters but the problem appears to be that UE4 is actively tracking the variables with the intention of using them for realloc. Is there somehow to prevent this type of behavior?

Add them to the Root Set (literally “MyUObject->AddToRootSet()” or something along those lines) as soon as you create them. When you want to delete them - remove them from the root set.

These are void pointers to standard types like int and bool.

Unless they’re UObjects - UE doesn’t track them. PODs like int/bool/float aren’t tracked - even if you call new on them. Sizeof(X) isn’t going to returned heap alloc’d memory size anyway so it can’t know about the data. You likely have something else going on.

So the pointer is stored in a ustruct and calling delete causes exceptions in ue4 specific classes like fmalloc.
also I’m aware that sizeof isn’t aware of the correct variable sizes which is what I think the source of the issue is. I’m personally static casting the void variables to the known type to delete.

You’d have to post the code at this point. Likely it’s just something weird with how you are freeing the memory, as you suspect.

I ended up just refactoring my code completely. Thanks for your help, you got me going in the right direction.