Solved

Problem about "delete this"

Posted on 1998-08-31
4
172 Views
Last Modified: 2013-11-19

When I read the Visaul C++ sample SAVER ,
I do not understand the meaning of the
following lines.


CDrawWnd* pWnd = new CDrawWnd();
.
.
.

CSaverWnd* pWnd = new CSaverWnd;
.
.
.

void CDrawWnd::PostNcDestroy()
{
      if (m_bAutoDelete)
            delete this;
}

How to use "delete this;" properly?


0
Comment
Question by:murphylee
  • 2
4 Comments
 
LVL 23

Expert Comment

by:chensu
ID: 1321548
CWnd::PostNcDestroy is called by the default CWnd::OnNcDestroy member function after the window has been destroyed. CWnd::OnNcDestroy is called by the framework when the nonclient area is being destroyed, and is the last member function called when the Windows window is destroyed. So, at this point the object attached to the window can be deleted. Otherwise it is difficult to know when the object can be deleted safely. If you look into the MFC source code, you'll notice that MFC uses this technique for the frame window.


0
 

Author Comment

by:murphylee
ID: 1321549
Hi,chensu
I understood what you said
but there still unclearly  is a easy and dumb question
According the following lines
 
class CSaverDlg : public CDialog
{



protected:
CDrawWnd m_wndPreview; // don't autodelete



}

class CDrawWnd : public CWnd
{
// Construction
public:
      CDrawWnd(BOOL bAutoDelete = TRUE);
.
}

Such lines demonstrate
in CSaverDlg  , m_wndPreview is not autodeleted , and
in CsaverWnd , it is autodeleted.
Why  they say so?
0
 
LVL 8

Accepted Solution

by:
Answers2000 earned 0 total points
ID: 1321550
1. CSaverDlg is the a class associated with a dialog box.  

When the dialog box variable goes out of scope (in this case it's a modal dialog so when you press OK or Cancel, and the calling function exits), all it's member variables are deleted too by the compiler generated code automatically (in this case m_wndPreview).

Incidentally the m_wndPreview is the "preview" window in screen save settings.


2. The other CDrawWnd is created using new.  Thus it will never go out of scope.  Therefore the C++ object will never be deleted automatically by the compiler generated code, hence the need for a manual delete.


Finally although MFC extensively uses "delete this" it's at the very least highly debatable whether this constitutes good code.  There is an argument that it would be better to put the delete outside the class, but I guess when you work with a framework, you (generally should) live by its rules.

0
 

Author Comment

by:murphylee
ID: 1321551
Thanks experts
I understood completely.
Just because putting the delete inside the class,
provide me a few misunderstandings.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
maxBlock challenge 30 111
format the code in java 6 86
withoutTen challenge 14 99
Problem to event 3 76
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now