Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Problem about "delete this"

Posted on 1998-08-31
4
Medium Priority
?
190 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

926 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