When to delete a pointer?

Hi…

As you know, C++ does not has an automatic garbage collector so i need to delete manually my pointers…

What happens with those pointers declared in .h ?



class UStaticMeshComponent * myMesh;


Do i have to destroy it in the destructor of the current class? or does UE takes automatically control of it for me ?

What happens in this case?




void AMyClass::MyFunction(){

     AActor * aSimpleActor = ASomeActor;   // supposing that ASomActor is an Actor declared on a header for the current source file.

]



I´m still confused using pointers in UE…
Thanks in advance…

UE takes care of garbage collection on any UObjects so you don’t have to worry about the destructor.
If you search for “Unreal Object Handling” you can find more information.

That sounds good, so, i suspect that i only need to delete pointes when creating primitive types like int, char, bool, etc… isn´t ?

Unless you’re doing something very unusual, you will only need UObject pointers marked as UPROPERTY()'s or UE4 smart pointers like TWeakPtr. In either case you will never need to manually delete anything. In my experience there are very few situations where you would ever need to use a raw pointer or manually manage memory in UE4. As long as you stick to one of the two solutions I mentioned you can (for the most part) consider UE4 C++ to be garbage collected.

Relevant documentation:

Thanks for lighting me.

You don’t need to delete pointers to primitive type either unless you used the “new” operator

that’s true Agin, but i guess there is no sense to use primitive type pointers without allocating Dynamic memory for them…
Thanks all

Of course pointers to primitive types are useful even if you don’t dynamically allocate memory.
That being said if you plan on doing a dynamic array using your pointer I’d recommend you use the TArray class