Solved

Problem about "delete this"

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
maxBlock challenge 30 100
sumNumber challenge 16 99
has22 challenge 11 72
java ^ examples 8 57
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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…
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

759 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

18 Experts available now in Live!

Get 1:1 Help Now