即使调用 Close 之后,CDatabase 也会泄漏

发布于 2024-11-06 01:26:14 字数 2209 浏览 1 评论 0原文

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

如何在不破坏 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

My application leaks while executing the following code piece. Memory is not released even after calling Close() on the CDatabase.

How can I correct the leak without destroying the CDatabase object?

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

The call stack of the leak location is given below

+  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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贩梦商人 2024-11-13 01:26:14

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

When your program opens the first database connection, it loads database libraries into memory. The following connections do not really need a lot of memory because of connection pooling. Closing all connections does not unload everything you loaded before in an instant, and some parts stay in memory until your program exits.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文