Thread: [Dev-C++] "delete this" in the class member
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Oleksandr Y. <erm...@ma...> - 2004-01-28 10:05:09
|
Hi all,
Little question about delete operator.
Can i fall into trouble when making a call "delete this" in the
class member function ?
Example:
struct C1
{
func()
{
/* some other calls */
delete this;
}
};
Of course, "delete this" call is the LAST operator in the function.
If there are no pitfalls, then is it compatible with ISO/ANSI C++?
Thank in advance,
Oleksandr mailto:erm...@ma...
|
|
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? |
|
From: Obi <ob...@ch...> - 2004-01-31 04:51:31
|
At 00:18 29/01/2004 +0000, Miguel Ram=EDrez J=E1vega wrote: >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: Also you need to be sure the instance is actually new-ed. If the instance is an automatic or static variable callinf "delete this" will have undefined behaviour, probably bad. If you want to "delete this" you are recommended to have private constructors and destructor. Use a factory method to create objects. |
|
From: Oleksandr Y. <erm...@ma...> - 2004-01-30 18:26:29
|
Hello Miguel,
Thursday, January 29, 2004, 3:18:56 AM, you wrote:
MRJ> 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 ?
>> =20
MRJ> What do you mean exactly by "falling into trouble"? Obviously, the f=
irst
MRJ> think one thinks at seeing that is the risk of dangling references.
Initially i saw this "delete this" technique in some OO library.
The purpose of using this techinque was to implement
self-deleting of object if there are no references left.
Something like that:
inline CGObject::AddRef()
{
return ++m_iRefCount;
}
inline CGObject::Release()
{
m_iRefCount--;
if (m_iRefCount=3D=3D0) //destroy object
{
delete this;
return 0;
}
return m_iRefCount;
} =20
MRJ> Any
MRJ> reference to that struct instance will be invalid.
Yes, of course. But there is some another problem.
This technique works well on objects contructed with "new"
operator.
But objects can have also "auto" and "static" memory class.
So, this technique must set restriction to construct objects of
such class only dynamically(using "new").
So, may be you can use "delete this" if you know exactly what you
are doing. But is this allowed in ISO/ANSI C++ ?
MRJ> And second, valgrind
MRJ> prompts the following when executing the snippet of code you sent:
MRJ> [...]
MRJ> The most important part is the 'Invalid free' line. I am unsure abou=
t
MRJ> this, but I would assume that it is a synonym of "heap corruption".
Very interesting. I haven't use valgrind at this problem.
I will try it soon.
MRJ> Seems that gcc guys care about people not shooting on their feet ;)
MRJ> Miguel.
MRJ> PS: Quite offtopic, but does anybody know if valgrind is available a=
s a
MRJ> cygwin package?
I don't know. But does valgrind group plan to produce some
*STABLE* release even for linux ? As far as i know it is available
only by CVS.
P.S.: Sorry for my english.
--=20
Best regards,
Oleksandr mailto:erm...@ma...
|
|
From: Miguel <mra...@iu...> - 2004-01-30 23:49:46
|
On Thu, 2004-01-29 at 14:12, Oleksandr Yermolenko wrote: Hi, > The purpose of using this techinque was to implement > self-deleting of object if there are no references left. > Something like that: Hmmm. I am a bit puzzled. Could you tell which one is this OO library? Who must derive from/delegate on CGObject? Or it is a Policy class, that becomes the template parameter of some other class? > But objects can have also "auto" and "static" memory class. > So, this technique must set restriction to construct objects of > such class only dynamically(using "new"). > So, may be you can use "delete this" if you know exactly what you > are doing. But is this allowed in ISO/ANSI C++ ? > This somehow reminds me of the problem with having a std::vector<std::auto_ptr<Foo> >. Remember that serious STL implementations forbid this. > I don't know. But does valgrind group plan to produce some > *STABLE* release even for linux ? As far as i know it is available > only by CVS. > Well, I use it at home with Gentoo (yeah I know, this is not fair play), and at work we have sort of 'patchwork Debian Sarge' and it works quite well also. To say the least I am truly impressed with both the default valgrind 'skin' and the calltree one ( and the humble KCachegrind program beats Rational Purify crap everywhere, every time). I don't aspire to get KDE programs run smoothly on Cygwin ( I tried it and I find quite boring to wait for widgets to repaint each time a dialog box pops up HIDEing and EXPOSEing whatever it has below :( ), but valgrind used from commandline would be a nice asset to add to Win32 debugging tools. > P.S.: Sorry for my english. Mine is worse :) Miguel. |
|
From: Oleksandr Y. <erm...@ma...> - 2004-01-30 18:50:05
|
Hello Miguel, Friday, January 30, 2004, 3:22:05 AM, you wrote: > Hmmm. I am a bit puzzled. Could you tell which one is this OO library? > Who must derive from/delegate on CGObject? Or it is a Policy class, that > becomes the template parameter of some other class? This library is GTL. http://www.gtl.kis.ru/bin/GTL10R8.exe This class with "delete this" is the template parameter of pointers "container" class. (see line #33 /Source/Gobject.cpp) This library is based on MFC :-( Therefore i have no plans to use it in real work, but some design concepts of this library are interesting(and may be some of them are dangerous :-) ). > [...], but valgrind > used from commandline would be a nice asset to add to Win32 debugging > tools. Yes, it will be useful tool. Among similar tools under Win32 i have used only CodeGuard (comes with Borland C++ Builder 5 or later(may be)). Valgrind under Win32 can be a good open source replacement. -- Best regards, Oleksandr mailto:erm...@ma... |
|
From: Oleksandr Y. <erm...@ma...> - 2004-01-31 19:51:38
|
Obi <ob...@ch...> wrote: > Also you need to be sure the instance is actually new-ed. If the instance > is an automatic or static variable callinf "delete this" will have undefined > behaviour, probably bad. > If you want to "delete this" you are recommended to have private > constructors and destructor. Use a factory method to create objects. Good idea. Thank you. Private constructor/destructors (friend'ed only for container of ptrs) will prevent incorrect using of this class. -- Best regards, Oleksandr mailto:erm...@ma... |
|
From: Oleksandr Y. <erm...@ma...> - 2004-01-31 20:02:16
|
Hello Miguel, Friday, January 30, 2004, 3:22:05 AM, you wrote: > Hmmm. I am a bit puzzled. Could you tell which one is this OO library? > Who must derive from/delegate on CGObject? Or it is a Policy class, that > becomes the template parameter of some other class? This library is GTL. http://www.gtl.kis.ru/bin/GTL10R8.exe This class with "delete this" is the template parameter of pointers "container" class. (see line #33 /Source/Gobject.cpp) This library is based on MFC :-( Therefore i have no plans to use it in real work, but some design concepts of this library are interesting(and may be some of them are dangerous :-) ). > [...], but valgrind > used from commandline would be a nice asset to add to Win32 debugging > tools. Yes, it will be useful tool. Among similar tools under Win32 i have used only CodeGuard (comes with Borland C++ Builder 5 or later(may be)). Valgrind under Win32 can be a good open source replacement. -- Best regards, Oleksandr mailto:erm...@ma... |