Is the class in the header without a pointer destroyed by itself?


Suppose we have a class: FSimpleClass. The following classes are declared in the SimpleClass header file:

class FExampleClass* ExampleClass1;

class FExampleClass ExampleClass2;

Both classes (variables) are private and are created / initialized inside FSimpleClass.

Suppose that the class FSimpleClass is created and destroyed somewhere during the game. We wrote the destruction of the class ExampleClass1 in the destructor, but do we need to destroy the class ExampleClass2? Will the memory spent on ExampleClass2 be freed after the destruction of FSimpleClass, if this is not done?

And also, is it necessary to write in the destructor:

ExampleClass1 = nullptr;

after delete ExampleClass1, that there would be no dangling pointer? Or the pointer will not be after the destruction of FSimpleClass?

If you are not deriving from UObject you should use delete in SimpleClass to delete a pointer of ExampleClass1, which should have a destructor. You don’t need to destroy ExampleClass2, you just need a valid destructor. When SimpleClass is destroyed, ExampleClass2 will be destroyed as well.

Thank. There is a need to do after delete ExampleClass1: ExampleClass1 = nullptr?

Calling delete should be enough.