如何破坏窗口并再次创建它?

时间:2015-05-17 22:49:36

标签: mfc

我创建了一个窗口 当我这样做时

w->Create (...);
w->DestroyWindow ();
w->Create (...);

程序崩溃了 任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

我们对您的MFC窗口类知之甚少。如果它是从CFrameWnd或CView派生的,它将崩溃。那是因为在那些窗口被破坏后,它们的C ++内存指针(this)不再有效了。它指向已被C ++内存分配器释放的内存区域。试图取消引用它会导致崩溃。因为,在PostNcDestroy()覆盖中,CFrameWnd和CView类调用“delete this”。

void CFrameWnd::PostNcDestroy()
{
    // default for frame windows is to allocate them on the heap
    //  the default post-cleanup is to 'delete this'.
    // never explicitly call 'delete' on a CFrameWnd, use DestroyWindow instead
    delete this;
}

void CView::PostNcDestroy()
{
    // default for views is to allocate them on the heap
    //  the default post-cleanup is to 'delete this'.
    //  never explicitly call 'delete' on a view
    delete this;
}