Does Unreal detroy childrens upon parent destruction?

Hello Guys,

I’m still working on memory management for our game and I wonder something.

Does Unreal is supposed to destroy automaticallyUPROPERTY()marked childrens objects (Actor or Object) pointers upon their destruction ?

Consider the folowing code :

.h

class AGame_API ADummyActor : public AActor
{
	GENERATED_BODY()
	
public:

	ADummyActor(const FObjectInitializer& ObjectInitializer);

	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = Dummy)
	UTexture* texture;

    void SetTexture();
};

.cpp

ADummyActor::ADummyActor(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{
	texture = nullptr;
}

void ADummyActor::SetTexture()
{
	texture = LoadObject<UTexture>(this, TEXT("/Game/Menu/Artwork/toto/Background_fantasy.Background_fantasy"), NULL, LOAD_None, NULL);
}

This code would be the same for a UObject derived object.

For this case, assume that I no (0) instance of the DummyActor and I have a button to spawn an instance and another to destroy that very same instance. Also, assume that the texture is not referenced anywhere else.

When I call delete upon the button, is it supposed to also destroy the texture pointer and release the memory associated with the texture ?

If YES, that doesn’t work at all.

If NO, should I declare a ClearChildrens function that will manually delet every childrens data ? or what sould I do ?

I mean in raw C++ there’s no garbage, so I’m well used to clean childrens memory upon destructor call.

But with Unreal, we are supposed to have a garbage collector that is supposed to clean unused memory right ?

Or am I crazy and missed something o0 ?

Thanks for any help…

gamer08

As far a I know Unreal doens’t destruct children on destruction of a parent since you can have children in a Level without a parent in the level. But I am not sure

Hello ,

Well, it’s something I would to have a confirmation from epic staff if possible because it’s not very clear on the documentation.

Hey guys,

I decided to call up a function to “clear childrens” before delete the parent and this with both actor and object. In the end it works, but I would really like to know if the GC is suppose to clear childrens automatically upon parent destruction specifically if they are marked UPROPERTY() ?

Thanks for a future answer.

gamer08