Question about freeing pointers

I noticed that we were creating various pointers as references to objects like the barrel. I was wondering why don’t we ever free them inside the destructor of the class they’re being declared in? Is it because the barrel is created by the UE4 engine and clean up is left to the engine? What about pointers we declare ourselves with new.

Also, from what I can recollect, isn’t keeping multiple instances of a pointer to the same object not ideal? I believe this is because keeping multiple pointers to the same object can be dangerous once the memory is freed. It’ll be difficult keeping track what has access to what pointer. Though in the case of this lecture it seems fine I believe since once the Tank is destroyed, so is the aiming component.

Thanks.

Yes.

You wouldn’t be using new. You’d either be using a smart pointer or creating game objects that are managed by Unreal.

The issue I believe you are referring to is double free/delete which won’t be happening here.

I see thank you for the reply!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms