Click to See Complete Forum and Search --> : delete this ?
sohail_k
August 19th, 2004, 09:14 AM
Hi everyone,
Can someone tell me what would happen if I do --
delete this;
In any member function (say foo() )
of a class (say class myClass)
and I call this function as -
myClass myObj;
myObj.foo();
Have a great day ahead!
usman999_1
August 19th, 2004, 09:49 AM
It will call the dtor of the this (also of base classes if any) then do a sortof free for the memory that that was the object this points to hold. If you dont access any instance specific features you should be ok. But i have yet to find a reason why anyone would do that :confused: .
Hope this helps,
Regards,
Usman.
usman999_1
August 19th, 2004, 09:55 AM
Hi everyone,
Can someone tell me what would happen if I do --
delete this;
In any member function (say foo() )
of a class (say class myClass)
and I call this function as -
myClass myObj;
myObj.foo();
Have a great day ahead!
Btw: Since you are not allocating memory using new myClass myObj calling delete for this is plain wrong.
MrViggy
August 19th, 2004, 03:44 PM
It will call the dtor of the this (also of base classes if any) then do a sortof free for the memory that that was the object this points to hold. If you dont access any instance specific features you should be ok. But i have yet to find a reason why anyone would do that :confused: .
Hope this helps,
Regards,
Usman.
It's actually a common technique in MFC modeless dialogs. What you do is:
void CSomeMenuHandler::CreateAndShowDlg()
{
CMyDlg *pMyDlg = new CMyDlg();
pMyDlg->Create(...);
pMyDlg->ShowWindow(...);
}
// In CMyDlg::PostNCDestroy
void CMyDlg::PostNCDestroy()
{
delete this;
}
You create the C++ class for the dialog (which contains your dialog specific handlers for your dialog items) and show the dialog by "new'ing" a pointer, but you don't know when that dialog will be closed by the end user. Instead of having to keep track of all of the dialog pointers, instead you handle the Windows "PostNCDestroy" message. This message gets sent when Windows is completely finished releasing all the dialog's resources. All that is left at this point is to "close" up the memory leak. So, you just "delete this!"
Viggy
sohail_k
August 20th, 2004, 02:05 AM
Hey Thanx Usman and MrViggy.
Look if in myfuncction foo()
I have some statements that access data member of object before aswell after
delete this;
What would happen?
MrViggy
August 20th, 2004, 12:54 PM
You'd have unexpected behavior. Most likely, you'd crash. As to when/where the crash occurs, that's anybody's guess.
First off, you shouldn't call "delete" on a stack varaible. I believe that the compiler will compile that, however you will corrupt the stack and the heap.
As for accessing deleted pointers:
char *pMyChar = new char[256];
strcpy(pMyChar, "Some Stuff");
delete [] pMyChar;
// Do some other stuff here
pMyChar[2] = 'A';
This is bad, and will most likely lead to a crash, somewhere in your app. Between the delete and the overwrite, the heap may have re-allocated that section of memory to something else. You're now overwriting that object.
Viggy
sohail_k
August 23rd, 2004, 02:55 AM
Hey thanx MrViggy
MrViggy
August 23rd, 2004, 11:21 AM
No problem!
Vig.
NMTop40
August 25th, 2004, 08:55 AM
It is used in reference-counted base-classes such as COM uses.
You have a method AddRef() which increments the number of references.
You have a method Release() which decrements the number of references.
When the number of references reduces to 0, the Release function calls delete this;
The base class must have a virtual destructor.
The reference count should also be mutable and the AddRef and Release methods should be const. This is so you can have reference-counting on const pointers too.
Often you will have a smart pointer class that does the referencing for you. ATL provides such smart pointers.
sohail_k
August 25th, 2004, 10:36 AM
hi thanks NMTop40,
I understand what you are saying. I have been using COM for sometime.
Thanks again. :)
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.