请问哪位在com中用过ado连接数据库程序中使用CoInitializeEx初始化并设置了两个_ConntionPtr,_Recordset类成员变量,运行后出现如下错误:
"0x77bfa61d"指令引用的"0x00000003"内存,该内存不能为"written"。要终止程序,请单击确定我逐步执行,发现在_ConntionPtr,_Recordset类成员变量createinstance、open时编译发出警告音,这两个变量在类构造函数里面已经初始化了,而且在CoUninitialize之前调用函数释放这两个变量, 不知道为什么还是出错,请各位帮忙看看,谢谢!
"0x77bfa61d"指令引用的"0x00000003"内存,该内存不能为"written"。要终止程序,请单击确定我逐步执行,发现在_ConntionPtr,_Recordset类成员变量createinstance、open时编译发出警告音,这两个变量在类构造函数里面已经初始化了,而且在CoUninitialize之前调用函数释放这两个变量, 不知道为什么还是出错,请各位帮忙看看,谢谢!
运行出exe可执行文件后关闭的时候出错
有没有可能是在最后释放两个指针的时候出错?
我对应该在哪里释放这两个指针不是很清楚
void CServerApp::CloseDatabase()
{
m_pRecordset->Close();
m_pRecordset = NULL;if(m_pConnection->State)
m_pConnection->Close();
m_pConnection = NULL;
}
m_pRecordset = NULL;
m_pConnection = NULL;
BOOL CServerApp::ConnectDatabase()
{
BOOL retFt = FALSE;
HRESULT hr;
m_pConnection = NULL;
try
{
hr = m_pConnection.CreateInstance(_uuidof(Connection));
if(SUCCEEDED(hr))
{
m_pConnection->Open("Provide = MADAORA; Data Source = ORADB; User ID = admin; Password = adminx", "", "" , adModeUnknown);
retFt = TRUE;
}
}
catch(_com_error e)
{
m_pConnection = NULL;
SOCmnString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s", e.ErrorMessage());
MessageBox(NULL, errormessage, "错误", MB_ICONEXCLAMATION);
retFt = FALSE;
}
return retFt;
}
BOOL CServerApp::BuildRecordset()
{
BOOL retFt = FALSE;
HRESULT hr;
m_pRecordset = NULL;
if(ConnectDatabase())
{
try
{
hr = m_pRecordset.CreateInstance(_uuidof(Recordset));
if(SUCCEEDED(hr))
{
m_pRecordset->Open("SELECT * FROM DATALIST",
m_pConnection.GetInterfacePtr(), adOpenDynamic,
adLockOptimistic,
adCmdText);
retFt = TRUE;
}
}
catch(_com_error e)
{
m_pRecordset = NULL;
SOCmnString errormessage;
errormessage.Format("错误编号:%08lx\n错误信息:%s\n错误源:%s\n错误描述:%s",
e.Error(), e.ErrorMessage(),
(LPCTSTR)e.Source(), (LPCTSTR)e.Description());
MessageBox(NULL, errormessage, "错误", MB_ICONEXCLAMATION);
retFt = FALSE;
}
}
return retFt;
} void CServerApp::CloseDatabase()
{
if(m_pRecordset->State != adStateClosed)
m_pRecordset->Close();
if(m_pConnection->State != adStateClosed)
m_pConnection->Close();
}
m_pConnection = NULL;
m_pRecordset = NULL;
智能指针和指针不一样,不要赋空值
2.
m_pConnection.CreateInstance
m_pRecordset.CreateInstance
只要在构造函数执行一遍就可以了
如果多次对其进行实例化将会出错.
m_pConnection.CreateInstance(_uuidof(Connection));
m_pRecordset.CreateInstance(_uuidof(Recordset));
错误编号:80004005
错误信息:未指定的错误
错误源:Microsoft OLE DB Provider for ODBC Drivers
错误描述:由于系统错误 1114 (Oracle in OraClient10g_home1)指定的驱动程序无法加载不知为什么会这样,还请指点,谢谢!