ADO关闭不释放所有内存 - 小泄漏

时间:2015-06-23 18:56:46

标签: c++ memory-leaks ado

我已经将我的代码隔离到了这一点 - 运行它会导致内存泄漏。谁能明白为什么?

HRESULT hResult = CoInitialize(NULL);
if ((hResult != S_OK) && (hResult != S_FALSE))
    return;

_ConnectionPtr conn; 
hResult = conn.CreateInstance(__uuidof(Connection));
if (hResult != S_OK)
    {
    CoUninitialize();
    return;
    }

conn->CursorLocation = adUseClient;
conn->Open(sConnectionString, L"", L"", adConnectUnspecified);

if (conn)
    {
    conn->Close();
    conn.Release();
    conn=NULL;
    }

CoUninitialize();
return;

这是sConnectionString,虽然我看不到任何会导致内存从conn对象泄漏的内容:

“Provider = Microsoft.SQLSERVER.CE.OLEDB.3.5; Data Source =”C:\ Temp \ acctprod.sdf“; ssce:database password ='password';”

1 个答案:

答案 0 :(得分:0)

使用CoInitializeEx和COINIT_MULTITHREADED而不仅仅是CoInitialize修复了泄漏。但我不确定何时/为何使用COINIT_MULTITHREADED或COINIT_APARTMENTTHREADED?

HRESULT hResult = CoInitializeEx(NULL,COINIT_MULTITHREADED);