Memory Leak, need help deleting

This code probably looks horrible, but I am trying to get rid of all the leaks. I don't know how to delete the dynamic memory I've created. At the end I call delete [] tables, and that frees up memory, but valgrind still reports several leaks.

Here is the code part (in a loop)

1
2
3
4
5
6
7
	Table *tables = new Table[MAX_TABLES];

         ...(in a loop reading a file)
              tables[i] = *(new Table(_tableId, _maxSeats));

        delete [] tables;
	return 0;
On line 4 you're assigning to an element of an array of Tables a copy of a dynamically allocated Table.

If you want an array of dynamically allocated Tables, create tables like this:
Table **tables = new Table*[MAX_TABLES];
and drop the asterisk on line 4. Of course, you'll have to iterate over the array to delete each Table individually.

If you just want an array of Tables, change the assignment on line 4 to this:
tables[i] = Table(_tableId, _maxSeats);
Do note, however, that this means you initialize each element of the array twice.
Thanks for the response, I already removed the new keyword and changed it like you have suggested. This fixes almost all the problems except in one class. I am unable to free another allocation. Even after calling the deconstructor, a leak remains. If I free the original waiter object similar to above, valgrind complains as an invalid free.

It is a double pointer, pointing to a list of tables.
Any suggestions?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Waiter
{
private:
	Table **tables;

public:
	Waiter(Table *table = NULL); 
	~Waiter();
};



Waiter::Waiter(Table *table)
{
	tables = new Table*[6]; <-- This isn't freeing properly
}

Waiter::~Waiter()
{
	delete [] tables;
} 



Delete each element of Waiter::tables. By the way, if you know the size of an array and it's fairly short, in general it's better to not allocate it dynamically: Table *tables[6];
I'm not sure how to do that, can you give me an example? Thanks in advance.
To elaborate on helios's (very valid) suggestion #1:

You're allocating an array of pointers to objects. Those objects are dynamically allocated, right? Are you remembering to free them too? If not, that's the most likely source of your leak. Merely deleting the array of pointers won't delete the pointed-to objects. You need to find a way to keep track of which pointers point to allocated objects, and delete them (the objects) when they're no longer needed.

-Albatross
Last edited on
Thanks for all the help so far. Yes, it is all dynamic. I still don't understand how to do it. In addition to the code above, I have been calling a delete on both tables and waiters objects at the end of main. Here are some snippets from main.

** I noticed that the deconstructors are being called right after statements like tables[i] = (Table(_tableId, _maxSeats));

1
2
3
4
5
6
7
8
9
10
11
12
13
Table *tables = new Table[MAX_TABLES];
Waiter *waiters = new Waiter[MAX_WAITERS];
.
.
tables[i] = (Table(_tableId, _maxSeats));
.
.
waiters[i] = (Waiter(_name, _tableList, tables));
.
.
delete [] tables;
delete [] waiters;
return 0;




Last edited on
Topic archived. No new replies allowed.