I am implementing a tree like data structure with a node base class and multiple derived classes. I am looking for a memory pool to allocate the various derived nodes with, but also properly destructs all the allocated nodes (as in via their destructors). For context, at some point in the program I want to destroy all the trees I have created hence why I think a memory pool would be a good way to deallocate all memory at once.
Previously I naively used boost::pool<>
and allocated the derived classes with it via new (pool.malloc()) ChildNode1(...)
before realising the pool had no concept of the data it was storing so naturally their destructors were not called and data with additional allocations like vectors/strings were not being cleaned up.
I looked at using boost::object_pool<Base>
however that does not compile as the Base class is virtual. Does boost pools not work with base classes? Is there some way of achieving my use case with boost pools?