This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Problem with delete[] in class destructor
- From: Nicolas Wack <nwack at iua dot upf dot es>
- To: gcc-help at gcc dot gnu dot org
- Date: Tue, 22 Mar 2005 10:53:16 +0100
- Subject: Problem with delete[] in class destructor
Hi all!
I have a little problem, and I thought maybe someone could help me out
before I go crazy!!
Here's a simple file that allocates some amount of memory (roughly
300MB), sleeps for 5 secs, deallocates it and sleeps for 15 seconds.
I know for sure that the program goes through all the destructors,
however nothing gets freed...
I can also reproduce this behavior (bug?) by replacing the Test class
with std::vector, for instance.
Also, calling v[i]->destroy() instead of deleting the object doesn't do
anything neither...
Can somebody tell me what's wrong? Is that a problem with gcc or the
libc or libstdc++?
Thanks in advance!
Nicolas.
My versions are:
debian unstable
linux 2.6.10
g++ 3.3.5 (debian version)
Configured with: ../src/configure -v
--enable-languages=c,c++,java,f77,pascal,objc,ada,treelang --prefix=/usr
--mandir=/usr/share/man --infodir=/usr/share/info
--with-gxx-include-dir=/usr/include/c++/3.3 --enable-shared
--enable-__cxa_atexit --with-system-zlib --enable-nls
--without-included-gettext --enable-clocale=gnu --enable-debug
--enable-java-gc=boehm --enable-java-awt=xlib --enable-objc-gc i486-linux
Thread model: posix
gcc version 3.3.5 (Debian 1:3.3.5-12)
glibc6 2.3.2.ds1-20
libstdc++ 1:3.3.5-12
#include <iostream>
#include <vector>
using namespace std;
const unsigned int N = 300000;
class Test {
float* _data;
public:
Test() { _data = new float[256]; }
~Test() { delete[] _data; }
void destroy() { delete[] _data; }
};
int main() {
vector<Test*> v(N);
for (uint i=0; i<N; i++) {
v[i] = new Test;
}
cout << "1st step" << endl;
sleep(5);
for (uint i=0; i<N; i++) {
delete v[i];
//v[i]->destroy();
}
cout << "2nd step" << endl;
sleep(15);
return 0;
}