Solved

Problem about "delete this"

Posted on 1998-08-31
4
176 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Decoding 32 bit binary streams 6 54
Perl Awk Need Help 3 139
zeroFront challenge 7 124
wordmultiple challenge 12 157
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

726 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