我的VC ++程序中存在内存泄漏(使用MS VS2008构建)。它是一个连接到MySql DB服务器的小测试应用程序(mysql ver 5.5.25a-log)。该应用程序使用的是mysql c连接器ver。 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.
答案 0 :(得分:0)
这是故意的,请参阅MySQL C Api Reference page:
调用mysql_library_init()和mysql_library_end()的目的是为MySQL库提供正确的初始化和最终化。对于与客户端库链接的应用程序,它们提供了改进的内存管理。如果不调用mysql_library_end(),则会分配一块内存。 (这不会增加应用程序使用的内存量,但有些内存泄漏检测器会抱怨它。)对于与嵌入式服务器链接的应用程序,这些调用会启动和停止服务器。
mysql_library_end
应正确释放使用的内存,同时最初显示为“泄漏”。关于bugs.mysql.com也有numerous bug次提交。