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.

解决方案 »

  1.   

    不是内存泄漏_ConnectionPtr m_pConnection = NULL;
      

  2.   


    try{
    m_pConnection.CreateInstance(__uuidof(Connection));
    }
    catch(_com_error e){
    CString str;
    str.Format(L"%s",(LPCTSTR)e.Description());
    AfxMessageBox(str);
    }
    看看打出什么错误信息
      

  3.   

    output 中显示如下
    First-chance exception in testADOConn.exe (KERNEL32.DLL): 0xC0000005: Access Violation.
      

  4.   

     m_pConnection=NULL; 加了也没用
      

  5.   

    try{
    m_pConnection.CreateInstance(__uuidof(Connection));
    }
    catch(_com_error e){
    CString str;
    str.Format(L"%s",(LPCTSTR)e.Description());
    AfxMessageBox(str);
    }
    捕获不到错误,可以继续执行