写了一个基于对话框的ADO程序,增加/删除记录等功能都没有问题,
就是退出程序(点击对话框右上角关闭或者点击退出按钮)的时候会报
“Debug Assertion Failed”“wincore.cpp Line980”之类的错。
跟踪了一下,就出在InitInstance()的最后一句“return FALSE”。在csdn上查找了一些关于退出时出错的帖子,都是在Close()出错,没有在return FALSE出错的。
所以还请高人指教,谢谢。BOOL CMydbApp::InitInstance()
{
m_pConnection=NULL; AfxEnableControlContainer();

AfxOleInit();
//::CoInitialize(NULL); HRESULT hr;
try
{
hr = m_pConnection.CreateInstance(__uuidof(Connection));
if(SUCCEEDED(hr))
{
m_pConnection->ConnectionTimeout = 10;///设置超时时间为10秒

if(m_pConnection->State)
m_pConnection->Close(); ///如果已经打开了连接则关闭它

m_pConnection->ConnectionString ="File Name=tongds.udl";
m_pConnection->Open("","","",NULL); 
m_cSuccess=true;
}

}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("连接数据库失败!\n请检查数据库是否启动或者连接是否正常。\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息 return FALSE;
} // Standard initialization
// If you are not using these features and wish to reduce the size
//  of your final executable, you should remove from the following
//  the specific initialization routines you do not need.#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif CMydbDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
//  dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
//  dismissed with Cancel
} // Since the dialog has been closed, return FALSE so that we exit the
//  application, rather than start the application's message pump.
//::CoUninitialize(); /*  不论是否屏蔽这几句,return FALSE都会报错。
if (m_pConnection->State)
m_pConnection->Close();
m_pConnection.Release();

AfxOleTerm();
        */
return FALSE;
}退出按钮不是用系统自己产生的那个,其代码如下
void CMydbDlg::OnCancel() 
{
// TODO: Add extra cleanup here
/* 不论有无这几句,到InitInstance都会在return FALSE出错
if (m_pRecordset!=NULL)
{
if (m_pRecordset->State)
m_pRecordset->Close();///关闭记录集
}
m_pRecordset.Release();
*/
CDialog::OnCancel();
}

解决方案 »

  1.   

    if (m_pConnection->State)
    m_pConnection->Close();
    m_pConnection=NULL;
      

  2.   

    你在InitInstance的最后如果返回FALSE程序就表示有错误啊。直接返回TRUE
      

  3.   

    你确认是程序走到return FALSE的时候报的错吗?调试一下,看看错误定位在哪一句
      

  4.   

    我在程序退出时使用OnDestroy();
    好像下面没有执行
    ------------------
    /*  不论是否屏蔽这几句,return FALSE都会报错。
    if (m_pConnection->State)
    m_pConnection->Close();
    m_pConnection.Release();
      

  5.   

    m_pConnection.Release();是不是执行了两次
      

  6.   

    to DebugXP():     
        这段我加上过(请看上面贴的源码),一样的错误
    to laiyiling(最熟悉的陌生人):
        因为InitInstance()产生的代码就这样写的。
        刚才我把它改成TRUE试了试,还是出错。
    to happyparrot(快乐鹦鹉):
        跟踪点击退出按钮后的流程是:
        1.CMydbDlg::OnCancel()
        2.CMydbApp::InitInstance()末尾,
          从if (nResponse == IDCANCEL)开始
        3.CMydbApp::ExitInstance()    我跟踪了很多次,都是到return FALSE就出错,进入不到ExitInstance()。
        Variables->Context下拉框中显示:
                MFC42D!5f42df64()
                MFC42D!5f4876e4()
                CMydbDlg::~CMydbDlg()
                CMydbApp::InitInstance()
                ......
      

  7.   

    to:dxb321(九天) 我在程序退出时使用OnDestroy();
       我照您所说修改如下   
       void CMydbDlg::OnDestroy() 
       {
    if (theApp.m_pConnection->State)
    theApp.m_pConnection->Close();
    theApp.m_pConnection=NULL; CDialog::OnDestroy();

    // TODO: Add your message handler code here
    delete this;
       }
       这次是从CMydbDlg::OnCancel进入到CMydbDlg::OnDestroy(),
       在CDialog::OnDestroy()一句出错。
       
    to laiyiling(最熟悉的陌生人):m_pConnection.Release();是不是执行了两次
       只有一次,另外一个是m_pRecordset.Release()。
       看到许多帖子上都说不用Release,会自动释放。
      

  8.   

    我在上面没有看到你用的Debugxp的方法啊,就是不用release,只是把他赋值为NULL
      

  9.   

    多谢各位,我今天早上发现是用的界面OCX的问题,辛苦大家了。结贴。