m_pConnection.CreateInstance时报 Access Violation,应该是内存泄漏,但后面Open Connectstr没问题程序能正常运行。这是怎么回事?
::CoInitialize(NULL);
HRESULT hr;
_ConnectionPtr m_pConnection; hr=m_pConnection.CreateInstance(__uuidof(Connection));
if (SUCCEEDED(hr))
{
AfxMessageBox("You are now Connected!",NULL,MB_OK); //Not Executed
}
else if(FAILED(hr))
AfxMessageBox("Not able to connect to DB!",NULL,MB_OK); //Executed
---------
output 中显示如下
First-chance exception in testADOConn.exe (KERNEL32.DLL): 0xC0000005: Access Violation.
::CoInitialize(NULL);
HRESULT hr;
_ConnectionPtr m_pConnection; hr=m_pConnection.CreateInstance(__uuidof(Connection));
if (SUCCEEDED(hr))
{
AfxMessageBox("You are now Connected!",NULL,MB_OK); //Not Executed
}
else if(FAILED(hr))
AfxMessageBox("Not able to connect to DB!",NULL,MB_OK); //Executed
---------
output 中显示如下
First-chance exception in testADOConn.exe (KERNEL32.DLL): 0xC0000005: Access Violation.
try{
m_pConnection.CreateInstance(__uuidof(Connection));
}
catch(_com_error e){
CString str;
str.Format(L"%s",(LPCTSTR)e.Description());
AfxMessageBox(str);
}
看看打出什么错误信息
First-chance exception in testADOConn.exe (KERNEL32.DLL): 0xC0000005: Access Violation.
m_pConnection.CreateInstance(__uuidof(Connection));
}
catch(_com_error e){
CString str;
str.Format(L"%s",(LPCTSTR)e.Description());
AfxMessageBox(str);
}
捕获不到错误,可以继续执行