I've made a linked list implementation based on a memory pool. But as I came to analyze the code, I got a strong feeling that it might leak and would need someone to confirm/deny it ;) Heres the snipped that I'm worried about :
struct tst
{
~tst() { std::cout << "\nTST Destructor\n"; }
};
template<class T>
struct elem
{
T value;
elem *next, *prev;
//my question deals mostly with these 2 members below
tst t;
tst *pt;
elem()
{ std::cout << "\nElem Constructor\n"; pt = new tst;}
~elem()
{ std::cout << "\nElem Destructor\n"; delete pt;}
};
template<class T>
class Lista
{
private:
elem<T> *first, *last;
void *memPool;
constint maxElemCount = 100;
void DeleteAll();
public:
Lista();
~Lista();
//(I'm not worried about the code used to insert the elements so I'm skipping it here)
};
template<class T>
Lista<T>::Lista() : first(nullptr), last(nullptr)
{
memPool = ::operatornew(sizeof(elem<T>) * maxElemCount);
}
template<typename T>
Lista<T>::~Lista()
{
delete memPool;
}
elem destructor doesnt seem to be called but delete memPool; deletes the memory area where all of them were created
So now :
1) Destructor of tst t member doesnt get called, but its allocated on a space reserved for an elem structure, so it gets cleaned at delete memPool;
2) Bigger problem would be that tst *pt doesnt get deallocated as the elem destructor doesnt get called, so I would have to change the delete memPool; line to a loop scrolling through elements and explicitly calling pointerToElement->~elem();