|
|
bash$ g++ -Wall -g -fno-inline test.cpp bash$ valgrind --leak-check=full ./a.out ==17968== Memcheck, a memory error detector ==17968== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al. ==17968== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info ==17968== Command: ./a.out ==17968== ==17968== Invalid free() / delete / delete[] ==17968== at 0x4025186: operator delete[](void*) (vg_replace_malloc.c:409) ==17968== by 0x80485F5: Atom::~Atom() (test.cpp:5) ==17968== by 0x80486E2: Frame::~Frame() (test.cpp:16) ==17968== by 0x8048592: main (test.cpp:27) ==17968== Address 0x42cc1d8 is 0 bytes inside a block of size 24 free'd ==17968== at 0x4025186: operator delete[](void*) (vg_replace_malloc.c:409) ==17968== by 0x80485F5: Atom::~Atom() (test.cpp:5) ==17968== by 0x8048586: main (test.cpp:27) ==17968== ==17968== ==17968== HEAP SUMMARY: ==17968== in use at exit: 24 bytes in 1 blocks ==17968== total heap usage: 7 allocs, 7 frees, 168 bytes allocated ==17968== ==17968== 24 bytes in 1 blocks are definitely lost in loss record 1 of 1 ==17968== at 0x4025FE5: operator new[](unsigned int) (vg_replace_malloc.c:299) ==17968== by 0x80485CF: Atom::Atom() (test.cpp:4) ==17968== by 0x8048630: Frame::Frame() (test.cpp:13) ==17968== by 0x804855A: main (test.cpp:25) ==17968== ==17968== LEAK SUMMARY: ==17968== definitely lost: 24 bytes in 1 blocks ==17968== indirectly lost: 0 bytes in 0 blocks ==17968== possibly lost: 0 bytes in 0 blocks ==17968== still reachable: 0 bytes in 0 blocks ==17968== suppressed: 0 bytes in 0 blocks ==17968== ==17968== For counts of detected and suppressed errors, rerun with: -v ==17968== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 19 from 8) bash$ |
_atoms[_natoms++] = a;
.double* x;
. By overwriting double* x;
you make the memory that it was pointing to inaccessible.
|
|
bash$ g++ -Wall -g -fno-inline test.cpp bash$ valgrind --leak-check=full ./a.out ==853== Memcheck, a memory error detector ==853== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al. ==853== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info ==853== Command: ./a.out ==853== ==853== ==853== HEAP SUMMARY: ==853== in use at exit: 0 bytes in 0 blocks ==853== total heap usage: 7 allocs, 7 frees, 168 bytes allocated ==853== ==853== All heap blocks were freed -- no leaks are possible ==853== ==853== For counts of detected and suppressed errors, rerun with: -v ==853== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 19 from 8) bash$ |
return 0;
at the end of your main function as well. I don't believe your destructor will be called otherwise if I remember right, I just started working with classes a few days ago. You could test this by adding a cout << "Destructor called." << endl;
in your destructor code.
|
|
return 0;
makes no difference on whether or not the destructor is called. I'm really not sure why I thought it did, I seem to remember reading it but I guess I was wrong.
int main()
without returning an integer, but in this I guess main() is an exception.