Does emptying a TMap destroy AActor* values?

I’m spawning actors and placing them in a TMap.

TMap<uint32, AActor*> actors;

At certain intervals, the map should be emptied, and none of the actors are necessary anymore. Before I call actors.Empty(), should I loop over the map and call Destroy() on all the stored actors? Same with wanting to call actors.Remove(0)–is is necessary that actors[0]->Destroy() is called before it?

Hello, sgp

Please note that when Empty() is called, it eventually destroys the allocated elements:

Element.~ElementType();

However, since the Map contains pointers to Actors, Desroy() should be called for every Actor.

Hope this helped!

Have a great day!

Thanks! Does this happen with Remove() too?

This doesn’t make sense to me. It is a map of pointers, surely it can’t be calling the destructor on the pointed-to elements, since it doesn’t own them? It should only be deallocating the pointer itself, no?

The behaviour I would have expected would be that (as of 4.8) removing elements from a map of UObjects just removed the reference, which would result in garbage collection destroying the object if there were no other references. In the case of AActor, this wouldn’t be enough, since every actor is referenced by the world, so it would be necessary to call Destroy() before removing an element from the map.

I kind of just took that answer without thinking about it, but you’re right. It looks like AActor doesn’t even have a destructor–nor do its parent classes up the hierarchy–so even if Empty() does ‘destroy’ the actor this way for some reason, calling the actual Destroy() function is probably still necessary.