直接上代码
void CDlgSpeechDlg::OnView() 
{
// TODO: Add your control notification handler code here
if(nView==0)
{
CRect rt;
GetWindowRect(&rt);//rt.right,rt.top//ScreenToClient(&rt);
::PlaySound(LPCTSTR(IDR_BEGIN),::AfxGetInstanceHandle(),SND_RESOURCE|SND_ASYNC);
CViewDlg *pDlg=new CViewDlg();
pDlg->Create(CViewDlg::IDD);
pDlg->SetWindowPos(NULL,rt.right,rt.top,0,0,SWP_NOSIZE);
pDlg->ShowWindow(SW_RESTORE);
nView=1;
}
else if(nView==1)
{
CViewDlg *pDlg;
pDlg=(CViewDlg*)AfxGetMainWnd()->GetDlgItem(IDD_VIEW);
pDlg->OnCancel();
nView=0;
}
}void CViewDlg::OnCancel()
{
DestroyWindow();
}创建新窗口的时候没问题。。关闭的时候一按就出错误内存错误。。
大家帮忙看看

解决方案 »

  1.   

    CViewDlg *pDlg=new CViewDlg(); 
    完了之后应该
    delete pDlg;
      

  2.   

    1. CViewDlg *pDlg=new CViewDlg();
        pDlg指针你最好缓存起来.2.  pDlg=(CViewDlg*)AfxGetMainWnd()->GetDlgItem(IDD_VIEW); 
        这里不一定能得到窗口IDD_VIEW的指针
      

  3.   

    void CViewDlg::PostNcDestroy() 
    {
    // TODO: Add your specialized code here and/or call the base class
    delete this;

    CDialog::PostNcDestroy();
    }
    PostNcDestroy()中delete
      

  4.   

    你应该用一个CViewDlg *m_pDlg全局变量;
    m_pDlg=new CViewDlg(); 
    m_pDlg->Create(CViewDlg::IDD); 
    m_pDlg->SetWindowPos(NULL,rt.right,rt.top,0,0,SWP_NOSIZE); 
    m_pDlg->ShowWindow(SW_RESTORE); 
    m_pDlg->OnCancel(); 
      

  5.   

    void CViewDlg::PostNcDestroy() 

    // TODO: Add your specialized code here and/or call the base class 
    delete this; //CDialog::PostNcDestroy(); 

      

  6.   

    我试了。。还是没有用啊
    大家帮帮忙啊。搞了半天都搞不懂。。它老是有这个错误
    DlgSpeech.exe 遇到问题需要关闭。我们对此引起的不便表示抱歉。
    大家帮我找找错误啊。。其他的都没问题,就这里
      

  7.   

    你析构函数里是不是有操作?有的话delete this 要放析构最后面
      

  8.   

    一般OnCancel都不进行重写的吧,你把OnCancel的重写部分屏蔽掉,试试用CViewDlg的默认OnCancel来进行对话框关闭,看看会不会出现问题。
      

  9.   

    void CViewDlg::OnCancel() 

       //DestroyWindow(); 
       CDialog::OnCancel();
    } try it.
      

  10.   


    Of course, there are some different ways to do it. But the simplest way is 全局变量.
    I think, you should do it. Otherwise, your program will have a memory leak problem.
      

  11.   

    Moreover, you should read this post. (请大侠耐心解决我的问题(关于VC销毁窗口对象),万分感激,谢谢VC/M_)Maybe it helps you to know more.请大侠耐心解决我的问题(关于VC销毁窗口对象),万分感激,谢谢VC/M
    http://topic.csdn.net/t/20000419/13/7215.html
      

  12.   

    EndDialog(1);
    在关闭时加上这个,然后,在析构函数中别忘了释放new的内存
      

  13.   

    试一下下面的,一般你在窗口在那里创建就在那里销毁
    // CDlgSpeechDlg.h中
    CViewDlg* m_pViewDlg;  // 在CDlgSpeechDlg中记得先要初始化为NULLvoid CDlgSpeechDlg::OnView() 

    // TODO: Add your control notification handler code here 
    if(nView==0) 

    CRect rt; 
    GetWindowRect(&rt);//rt.right,rt.top//ScreenToClient(&rt); 
    ::PlaySound(LPCTSTR(IDR_BEGIN),::AfxGetInstanceHandle(),SND_RESOURCE|SND_ASYNC); if ( m_pViewDlg != NULL && m_pViewDlg.GetSafeHwnd() ) // 窗口自己关闭了
    {
        delete m_pViewDlg;
        m_pViewDlg = NULL;
    }if ( m_pViewDlg == NULL )
    {
        m_pViewDlg = new CViewDlg();
        m_pViewDlg->Create(CViewDlg::IDD);
    }
    m_pViewDlg->SetWindowPos(NULL,rt.right,rt.top,0,0,SWP_NOSIZE); 
    m_pViewDlg->ShowWindow(SW_RESTORE); 
    nView=1; 

    else if(nView==1) 

    if ( m_pViewDlg != NULL )
    {
        if ( m_pViewDlg.GetSafeHwnd() )
            ::DestroyWindow( m_pViewDlg.GetSafeHwnd() );
        delete m_pViewDlg;
        m_pViewDlg = NULL;
    }nView=0; 

    } // 这个就不用了,去掉
    void CViewDlg::OnCancel() 

    //DestroyWindow(); 

      

  14.   

    有了
    {if ( m_pViewDlg!= NULL ) 
    {if ( m_pViewDlg.GetSafeHwnd() ) 
            ::DestroyWindow( m_pViewDlg.GetSafeHwnd() ); 
        delete m_pViewDlg; //你这里多DELETE了一次。。去掉就没事了
        m_pViewDlg= NULL; 
    } 你这里多DELETE了一次