?
Solved

Problem about "delete this"

Posted on 1998-08-31
4
Medium Priority
?
181 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

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 …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

801 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