Microsoft KB Archive/105081: Difference between revisions

From BetaArchive Wiki
(importing KB archive)
 
m (Text replacement - """ to """)
 
Line 12: Line 12:
<div id="TitleRow">
<div id="TitleRow">


= <span id="KB105081"></span>PRB: &quot;Warning: Calling DestroyWindow in CWnd::~CWnd&quot; Message =
= <span id="KB105081"></span>PRB: "Warning: Calling DestroyWindow in CWnd::~CWnd" Message =




Line 77: Line 77:
<br />
<br />


* The programmer has most likely called &quot;delete&quot; on a CWnd object rather than DestroyWindow().
* The programmer has most likely called "delete" on a CWnd object rather than DestroyWindow().
* An object derived from CWnd is not performing a DestroyWindow() call in its destructor.
* An object derived from CWnd is not performing a DestroyWindow() call in its destructor.
* Because the DestroyWindow() call is executed in the destructor for CWnd, only the CWnd portion of the CWnd-derived object is remaining when the DestroyWindow() is called. Thus, if you have a CWnd-derived object and receive this warning, the derived object's virtual functions will not exist and the CWnd functions will be called instead.
* Because the DestroyWindow() call is executed in the destructor for CWnd, only the CWnd portion of the CWnd-derived object is remaining when the DestroyWindow() is called. Thus, if you have a CWnd-derived object and receive this warning, the derived object's virtual functions will not exist and the CWnd functions will be called instead.
Line 90: Line 90:
<br />
<br />


<pre class="fixed_text">  To destroy a C++ Windows object, use DestroyWindow, not &quot;delete&quot;.
<pre class="fixed_text">  To destroy a C++ Windows object, use DestroyWindow, not "delete".
                 </pre>
                 </pre>
<br />
<br />

Latest revision as of 09:43, 20 July 2020

Knowledge Base


PRB: "Warning: Calling DestroyWindow in CWnd::~CWnd" Message

Article ID: 105081

Article Last Modified on 11/21/2006



APPLIES TO

  • Microsoft Foundation Class Library 4.2, when used with:
    • Microsoft Visual C++ 6.1
    • Microsoft Visual C++ 6.0 Professional Edition
    • Microsoft Visual C++ 6.0 Enterprise Edition
    • Microsoft Visual C++ 5.0 Learning Edition



This article was previously published under Q105081


SYMPTOMS

The following warnings are displayed when a debug version of a program is executed and TRACE() diagnostics are enabled:

   Warning: calling DestroyWindow in CWnd::~CWnd
   OnDestroy or PostNcDestroy in derived class will not be called
                

CAUSE

The above warnings are displayed by the CWnd::~CWnd() destructor. The warnings indicate the following:

  • The programmer has most likely called "delete" on a CWnd object rather than DestroyWindow().
  • An object derived from CWnd is not performing a DestroyWindow() call in its destructor.
  • Because the DestroyWindow() call is executed in the destructor for CWnd, only the CWnd portion of the CWnd-derived object is remaining when the DestroyWindow() is called. Thus, if you have a CWnd-derived object and receive this warning, the derived object's virtual functions will not exist and the CWnd functions will be called instead.


RESOLUTION

MFC Technote #17 contains more information on how to properly destroy a Window object and states the following cardinal rule:

   To destroy a C++ Windows object, use DestroyWindow, not "delete".
                



Also, examining CWnd::~CWnd() will reveal that the foremost task of the destructor is to ensure that the associated Windows window object has been destroyed. If a program calls DestroyWindow() before the CWnd destructor is called, the m_hWnd data member will be NULL, the TRACE0() messages will not occur, and DestroyWindow() will not be called in CWnd::~CWnd().

Keywords: kbprb kbwndw KB105081