Hi all,
Problem:
I try to deallocate memory pointed by pointer items of an STL list.
This should work fine but in my case, there can be duplicate pointers in the list and I get a double dealloc exception even though I test whether the pointer is NULL or not (see source code below). How can I solve this problem ?
Environment:
- Debian5 Lenny
- gcc version 4.3.2 (Debian 4.3.2-1.1)
- libc-2.7.so
- libstdc++.so.6.0.10
- Eclipse Galileo Build id: 20100218-1602 / CDT.
C++ source code:
list<Line *> * l = new list<Line *>;
Line * line = new Line(10, 10, 10, 10);
l->push_back(line);
l->push_back(line);
cout << "line addr " << line << endl;
for (list<Line *>::iterator it = l->begin(); it != l->end(); it++)
{
if (*it != NULL)
{
cout << "line it " << *it << " " << (*it)->toString() << endl;
delete (*it);
(*it) = NULL;
}
}
l->clear();
Error displayed:
*** glibc detected *** /home/debian/workspace/Scrap/Release/Scrap: double free or corruption (!prev): 0x0846de20 ***
======= Backtrace: =========
/lib/i686/cmov/libc.so.6[0xb6d68764]
/lib/i686/cmov/libc.so.6(cfree+0x96)[0xb6d6a966]
/usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0xb6f402e1]
/home/debian/workspace/Scrap/Release/Scrap[0x8067cb0]
/lib/i686/cmov/libc.so.6(__libc_start_main+0xe5)[0xb6d10455]
/home/debian/workspace/Scrap/Release/Scrap(_ZNSt8ios_base4InitD1Ev+0x49)[0x8052cd1]
======= Memory map: ========
08048000-0842c000 r-xp 00000000 08:01 3819374 /home/debian/workspace/Scrap/Release/Scrap
0842c000-08451000 rw-p 003e3000 08:01 3819374 /home/debian/workspace/Scrap/Release/Scrap