CDatabase在调用Close后仍然泄漏

时间:2011-05-11 06:11:49

标签: c++ mfc memory-leaks 64-bit

我的应用程序在执行以下代码段时泄漏。即使在Close()上调用CDatabase后,内存也不会被释放。

如何在不破坏CDatabase对象的情况下纠正泄漏?

CDatabase db;
int count = 2000;
for (int i=0; i <count; ++i)
{
    BOOL bRes = db.OpenEx("DSN=ICEDBServer;UID=sa;PWD=ICEConnect200");
    db.Close();
}

泄漏位置的调用堆栈如下所示

+  133154 ( 133154 -      0)   2001 allocs  BackTrace477
+    2001 (   2001 -      0)    BackTrace477    allocations

    ntdll!RtlAllocateHeap+00001292
    ntdll!LdrpCopyUnicodeString+000000B1
    ntdll!LdrpResolveDllName+000002CE
    ntdll!LdrpMapDll+000002C1
    ntdll!LdrpLoadDll+00000251
    ntdll!LdrLoadDll+000001C8
    kernel32!LoadLibraryExW+0000024D
    ODBC32!LoadDriver+00000235
    ODBC32!SQLDriverConnectW+00000C11
    ODBC32!SQLDriverConnect+000001BB
    mfc90!CDatabase::Connect+0000009E (f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\dbcore.cpp, 745)
    mfc90!CDatabase::OpenEx+00000089 (f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\dbcore.cpp, 289)
    testApp!CtestAppDlg::OnBnClickedButton1+0000006A (d:\test\testapp\testapp\testappdlg.cpp, 162)
    mfc90!_AfxDispatchCmdMsg+000000C4 (f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\cmdtarg.cpp, 82)
    mfc90!CCmdTarget::OnCmdMsg+000001A4 (f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\cmdtarg.cpp, 381)
    mfc90!CPropertySheet::OnCmdMsg+00000032 (f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\dlgprop.cpp, 814)
    mfc90!CWnd::OnCommand+00000100 (f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\wincore.cpp, 2364)
    mfc90!CWnd::OnWndMsg+0000005F (f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\wincore.cpp, 1769)
    mfc90!CWnd::WindowProc+00000038 (f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\wincore.cpp, 1755)
    mfc90!AfxCallWndProc+00000100 (f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\wincore.cpp, 240)
    mfc90!AfxWndProc+00000059 (f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\wincore.cpp, 402)
    mfc90!AfxWndProcBase+00000053 (f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\afxstate.cpp, 441)
    USER32!UserCallWinProcCheckWow+000001F9
    USER32!DispatchClientMessage+000000C3
    USER32!_fnDWORD+0000002D
    ntdll!KiUserCallbackDispatcherContinue+00000000
    USER32!ZwUserMessageCall+0000000A

1 个答案:

答案 0 :(得分:0)

当程序打开第一个数据库连接时,它会将数据库库加载到内存中。由于连接池,以下连接实际上并不需要大量内存。关闭所有连接不会立即卸载您之前加载的所有内容,并且某些部分将保留在内存中,直到您的程序退出。