Hi, I’m checking out the garbage collection section at the C++ Programming Guide.
I’ve reached the section where it says: that any property without the UPROPERTY() mark it’ll be deleted by the garbaged collector because it’s not in the root set.
Does this mean I need to use this mark in every variable or otherwise it’ll just be deleted by the GC?
No it is not required for every variable, only for those that need to be visible for UE. For example if you want to have a variable editable in the UE editor it needs to be a UPROPERTY, but if it is just a internal varaible for a class it is not required.
At this point I am not sure, but since a UObject cán be created on the heap it is a problem if its value gets out of scope, because then you can’t deallocate it’s memory. The GC probably does that for you if it is a UPROPERTY. if you have a pointer (on the stack) to it, and it goes out of scope the pointer is deallocated by c++, and the GC would take care of the UObject (if needed) since all uobject values would be uproperty then.
To more specificly answer your question about GC: “In a nutshell, Garbage Collection (GC) traverses the object hierarchy through designated UPROPERTY’s, starting from the root objects (hence AddToRoot and RemoveFromRoot methods). Any object that cannot be reached through this traversal will be garbage collected. Accordingly, non-UPROPERTY variables are not counted by the GC system.” (A new, community-hosted Unreal Engine Wiki - Announcements - Epic Developer Community Forums)
So UPROPERTY means it can be GCed if there is no reference to it, a non uporpterty can’t
As i understood, actually UPROPERTY macro ensures that the object declared in the pointer is
considered to be referenced, that means, that property with that macro WON’T be garbage-collected.