写了一个基于对话框的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();
}
就是退出程序(点击对话框右上角关闭或者点击退出按钮)的时候会报
“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();
}
解决方案 »
- MoveWindow(不好意思 分散完了 希望好心人帮帮忙解决下问题)
- 做的是一个MFC的系统托盘程序,有问题上来请教。
- 菜鸟问题:如何手动撤消单例模式中的内存空间!避免Singleton理论中的内存泄漏? (参与有分)
- 请教高手:如何从文件里读出的数据筛选出我所需要的数据?
- activex中如何加入其它的用户自己编写的activex空间,并创建控件数组?
- 直方图与文件夹中文件遍历
- 请问各位,在VC中如何嵌入汇编语言,急,急!
- 在回调函数中怎么得到对话框的句柄?
- 一个关于串口通信的问题。
- 关于atoi的运算!
- 急问:在内存中锁定ActiveX控件的问题!(分不够再加!!!)
- 求一个多线程串口通讯的编程,急!
m_pConnection->Close();
m_pConnection=NULL;
好像下面没有执行
------------------
/* 不论是否屏蔽这几句,return FALSE都会报错。
if (m_pConnection->State)
m_pConnection->Close();
m_pConnection.Release();
这段我加上过(请看上面贴的源码),一样的错误
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()
......
我照您所说修改如下
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,会自动释放。