在线:窗口删除时出现警告:calling DestroyWindow in CWnd::~CWnd; OnDestroy or PostNcDestroy in derived class will not be called.

QunKangLi 2003-09-19 10:33:04
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().
...全文
1092 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
milson 2003-09-19
  • 打赏
  • 举报
回复
我按照你的方法也做了一个没有问题啊。
// 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

/////////////////////////////////////////////////////////////////////////////
// CNewWnd

CNewWnd::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 handlers

BOOL 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 window

class 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

/////////////////////////////////////////////////////////////////////////////
// CTest001View

IMPLEMENT_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/destruction

CTest001View::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 drawing

void CTest001View::OnDraw(CDC* pDC)
{
CTest001Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CTest001View printing

BOOL 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 handlers

int 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();
}
QunKangLi 2003-09-19
  • 打赏
  • 举报
回复
我试过了,不管是在控件的析构函数还是视图类的析构函数中加入DestroyWindow,都不能消除警告
flyelf 2003-09-19
  • 打赏
  • 举报
回复
需要自己在析构函数中调用DestroyWindow
QunKangLi 2003-09-19
  • 打赏
  • 举报
回复
flyelf(空谷清音) 一语中的,只是太过...让我差点无从下手.
milson(ifaq) 新建仅有与问题相关代码的程序来进行测试,给了我很大启发!
我逐步删除控件中的功能性代码,发现问题在于控件在Create()中附带建立了一个动态文本提示窗(鼠标指向某界面元素后弹出并显示功能说明),但在析构时仅调用了自身的DestroyWindow().编译器所警告的是该提示窗存在隐患而不是控件自身.
感谢二位!

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧