Memory pools and automatic variables

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 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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;
	const int 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 = ::operator new(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();

Am I right?
Topic archived. No new replies allowed.