Warning: calling DestroyWindow in CWnd::~CWnd; OnDestroy or PostNcDestroy in derived class will not be called.
SDI程序在OnCreate()中使用new分配了一个派生自CWnd的自定义控件,并调用该类的Create进行创建,在~CMyView()中使用delete删除控件时出现如上所示警告.
将控件设为视图类的成员变量使用(即不用new,delete),警告同样出现.
附:控件中注册了一个窗口类,其Create()为创建该类窗口的一个实例.没有处理WM_DESTROY消息,没有重载PostNcDestroy,DestroyWindow().

解决方案 »

  1.   

    需要自己在析构函数中调用DestroyWindow
      

  2.   

    我试过了,不管是在控件的析构函数还是视图类的析构函数中加入DestroyWindow,都不能消除警告
      

  3.   

    我按照你的方法也做了一个没有问题啊。
    // NewWnd.cpp : implementation file
    //#include "stdafx.h"
    #include "Test001.h"
    #include "NewWnd.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CNewWndCNewWnd::CNewWnd()
    {
    }CNewWnd::~CNewWnd()
    {
    }
    BEGIN_MESSAGE_MAP(CNewWnd, CWnd)
    //{{AFX_MSG_MAP(CNewWnd)
    // NOTE - the ClassWizard will add and remove mapping macros here.
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    /////////////////////////////////////////////////////////////////////////////
    // CNewWnd message handlersBOOL CNewWnd::Create(LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
    {
    // TODO: Add your specialized code here and/or call the base class
    LPCTSTR lpszClassName = AfxRegisterWndClass(0); 
    return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
    }
    #if !defined(AFX_NEWWND_H__6013D12F_4DDA_4540_A14D_A4AD93D2A501__INCLUDED_)
    #define AFX_NEWWND_H__6013D12F_4DDA_4540_A14D_A4AD93D2A501__INCLUDED_#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    // NewWnd.h : header file
    ///////////////////////////////////////////////////////////////////////////////
    // CNewWnd windowclass CNewWnd : public CWnd
    {
    // Construction
    public:
    CNewWnd();// Attributes
    public:// Operations
    public:// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CNewWnd)
    public:
    virtual BOOL Create(LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext = NULL);
    //}}AFX_VIRTUAL// Implementation
    public:
    virtual ~CNewWnd(); // Generated message map functions
    protected:
    //{{AFX_MSG(CNewWnd)
    // NOTE - the ClassWizard will add and remove member functions here.
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    };///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_NEWWND_H__6013D12F_4DDA_4540_A14D_A4AD93D2A501__INCLUDED_)
    // Test001View.cpp : implementation of the CTest001View class
    //#include "stdafx.h"
    #include "Test001.h"#include "Test001Doc.h"#include "newwnd.h"
    #include "Test001View.h"
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CTest001ViewIMPLEMENT_DYNCREATE(CTest001View, CView)BEGIN_MESSAGE_MAP(CTest001View, CView)
    //{{AFX_MSG_MAP(CTest001View)
    ON_WM_CREATE()
    //}}AFX_MSG_MAP
    // Standard printing commands
    ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
    ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
    ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CTest001View construction/destructionCTest001View::CTest001View()
    {
    // TODO: add construction code here}CTest001View::~CTest001View()
    {
    delete m_pNewWnd;
    }BOOL CTest001View::PreCreateWindow(CREATESTRUCT& cs)
    {
    // TODO: Modify the Window class or styles here by modifying
    //  the CREATESTRUCT cs return CView::PreCreateWindow(cs);
    }/////////////////////////////////////////////////////////////////////////////
    // CTest001View drawingvoid CTest001View::OnDraw(CDC* pDC)
    {
    CTest001Doc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    // TODO: add draw code for native data here
    }/////////////////////////////////////////////////////////////////////////////
    // CTest001View printingBOOL CTest001View::OnPreparePrinting(CPrintInfo* pInfo)
    {
    // default preparation
    return DoPreparePrinting(pInfo);
    }void CTest001View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
    {
    // TODO: add extra initialization before printing
    }void CTest001View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
    {
    // TODO: add cleanup after printing
    }/////////////////////////////////////////////////////////////////////////////
    // CTest001View diagnostics#ifdef _DEBUG
    void CTest001View::AssertValid() const
    {
    CView::AssertValid();
    }void CTest001View::Dump(CDumpContext& dc) const
    {
    CView::Dump(dc);
    }CTest001Doc* CTest001View::GetDocument() // non-debug version is inline
    {
    ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTest001Doc)));
    return (CTest001Doc*)m_pDocument;
    }
    #endif //_DEBUG/////////////////////////////////////////////////////////////////////////////
    // CTest001View message handlersint CTest001View::OnCreate(LPCREATESTRUCT lpCreateStruct) 
    {
    if (CView::OnCreate(lpCreateStruct) == -1)
    return -1;

    // TODO: Add your specialized creation code here
    m_pNewWnd = new CNewWnd;
    m_pNewWnd->Create("New Window", WS_CHILD|WS_VISIBLE|WS_BORDER, CRect(0,0,100,100), this, 1000);
    m_pNewWnd->ShowWindow(SW_SHOW);

    return 0;
    }BOOL CTest001View::DestroyWindow() 
    {
    // TODO: Add your specialized code here and/or call the base class
    m_pNewWnd->DestroyWindow();
    return CView::DestroyWindow();
    }
      

  4.   

    flyelf(空谷清音) 一语中的,只是太过...让我差点无从下手.
    milson(ifaq) 新建仅有与问题相关代码的程序来进行测试,给了我很大启发!
    我逐步删除控件中的功能性代码,发现问题在于控件在Create()中附带建立了一个动态文本提示窗(鼠标指向某界面元素后弹出并显示功能说明),但在析构时仅调用了自身的DestroyWindow().编译器所警告的是该提示窗存在隐患而不是控件自身.
    感谢二位!