0

我的 VC++ 程序(使用 MS VS2008 构建)中存在内存泄漏。这是一个连接到 MySql DB 服务器(mysql ver 5.5.25a-log)的小型测试应用程序。该应用程序正在使用 mysql c 连接器版本。6.0.2。(与 'mysqlclient.lib' 链接),它的工作原理只是找到但有 124 字节的内存泄漏。可视泄漏检测器的调用堆栈将其跟踪到“my_thr_init.c”文件。我的代码:

MYSQL realData;
MYSQL *myData=NULL;

//this is done in InitInstance
    myData = mysql_init(&realData);
    if ( mysql_real_connect( myData, ip_addr, user, pwd, NULL,0,NULL,0))
    {
        AfxMessageBox(_T("Connected!"));
    }
    else {
        AfxMessageBox(mysql_error(myData));
    }

//and this is done in ExitInctance
    if(myData!=NULL)  {
        TRACE("ExitInstance  - closing myData\n");
        mysql_close( myData );
        mysql_library_end();
        myData=NULL;
    }

当应用程序关闭时,这是来自输出窗口:

    ...
ExitInstance  - closing myData
    Detected memory leaks!
    Dumping objects ->
    {2720} normal block at 0x05AAB830, 124 bytes long.
     Data: <        0;+     > 02 00 00 00 00 00 00 00 30 3B 2B 01 FF FF FF FF 
    Object dump complete.
    The thread 'Win32 Thread' (0x98c) has exited with code 0 (0x0).
    The thread 'Win32 Thread' (0x828) has exited with code 0 (0x0).
    The thread 'Win32 Thread' (0x424) has exited with code 0 (0x0).
    The thread 'Win32 Thread' (0xb6c) has exited with code 0 (0x0).
    The thread 'Win32 Thread' (0xdcc) has exited with code 0 (0x0).
    WARNING: Visual Leak Detector detected memory leaks!
    ---------- Block 2711 at 0x05AAB830: 124 bytes ----------
      Call Stack:
        g:\bs\connector-c-vs2008-32bit\src\mysql-connector-c-6.0.2\mysys\my_thr_init.c (330): HCCAdmin.exe!my_thread_init + 0x9 bytes
        f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\thrdcore.cpp (109): HCCAdmin.exe!_AfxThreadEntry + 0xF bytes
        f:\dd\vctools\crt_bld\self_x86\crt\src\threadex.c (348): HCCAdmin.exe!_callthreadstartex + 0xF bytes
        f:\dd\vctools\crt_bld\self_x86\crt\src\threadex.c (331): HCCAdmin.exe!_threadstartex
        0x74AD33AA (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
        0x77139EF2 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x63 bytes
        0x77139EC5 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x36 bytes
      Data:
        02 00 00 00    00 00 00 00    30 3B 2B 01    FF FF FF FF     ........ 0;+.....
        00 00 00 00    00 00 00 00    00 00 00 00    00 00 00 00     ........ ........
        00 00 00 00    DC 01 00 00    E4 01 00 00    F0 01 00 00     ........ ........
        F8 3A 2B 01    FF FF FF FF    00 00 00 00    00 00 00 00     .:+..... ........
        00 00 00 00    00 00 00 00    00 00 00 00    00 00 00 00     ........ ........
        34 0C 00 00    01 00 00 00    00 00 00 00    00 00 00 00     4....... ........
        01 00 00 00    00 00 00 00    00 00 00 00    00 00 00 00     ........ ........
        00 00 00 00    3C F7 4A 06    00 00 00 00                    ....<.J. ........


    Visual Leak Detector detected 1 memory leak (10178 bytes).
    Largest number used: 219192 bytes.
    Total allocations: 2113741 bytes.
    Visual Leak Detector is now exiting.
4

1 回答 1

0

这是故意的,请参阅MySQL C Api 参考页面

调用 mysql_library_init() 和 mysql_library_end() 的目的是提供 MySQL 库的正确初始化和最终确定。对于与客户端库链接的应用程序,它们提供了改进的内存管理。如果你不调用 mysql_library_end(),一块内存仍然被分配。(这不会增加应用程序使用的内存量,但一些内存泄漏检测器会抱怨它。)对于与嵌入式服务器链接的应用程序,这些调用启动和停止服务器。

应该正确释放使用的mysql_library_end内存,而最初显示为“泄漏”。bugs.mysql.com 上也有很多 关于此的错误提交。

于 2013-02-08T10:39:47.340 回答