Re: [Dev-C++] "delete this" in the class member
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Miguel <mra...@iu...> - 2004-01-29 00:28:52
|
On Wed, 2004-01-28 at 09:04, Oleksandr Yermolenko wrote: > Hi all, > Little question about delete operator. > Can i fall into trouble when making a call "delete this" in the > class member function ? > What do you mean exactly by "falling into trouble"? Obviously, the first think one thinks at seeing that is the risk of dangling references. Any reference to that struct instance will be invalid. And second, valgrind prompts the following when executing the snippet of code you sent: ==1912== Memcheck, a.k.a. Valgrind, a memory error detector for x86-linux. ==1912== Copyright (C) 2002-2003, and GNU GPL'd, by Julian Seward. ==1912== Using valgrind-2.0.0, a program supervision framework for x86-linux. ==1912== Copyright (C) 2000-2003, and GNU GPL'd, by Julian Seward. ==1912== Estimated CPU clock rate is 1610 MHz ==1912== For more details, rerun with: -v ==1912== ==1912== Invalid free() / delete / delete[] ==1912== at 0x40029DF1: __builtin_delete (in /usr/lib/valgrind/vgskin_memcheck.so) ==1912== by 0x40029E1C: operator delete(void*) (in /usr/lib/valgrind/vgskin_memcheck.so) ==1912== by 0x804846A: foo::func() (in /home/bowman/Sandboxes/Experiments/delete_myself) ==1912== by 0x8048452: main (in /home/bowman/Sandboxes/Experiments/delete_myself) ==1912== Address 0xBFFFF517 is on thread 1's stack ==1912== ==1912== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0) ==1912== malloc/free: in use at exit: 0 bytes in 0 blocks. ==1912== malloc/free: 1 allocs, 2 frees, 2 bytes allocated. ==1912== For a detailed leak analysis, rerun with: --leak-check=yes ==1912== For counts of detected errors, rerun with: -v The most important part is the 'Invalid free' line. I am unsure about this, but I would assume that it is a synonym of "heap corruption". Seems that gcc guys care about people not shooting on their feet ;) Miguel. PS: Quite offtopic, but does anybody know if valgrind is available as a cygwin package? |