valgrind报告mysql_real_connect的泄漏

时间:2013-05-28 10:42:22

标签: c++ mysql memory-leaks valgrind

我有以下代码结构(整个代码很大,我认为这个代码段与我的问题相关),

MYSQL_RES *res_set;
MYSQL_ROW row;
MYSQL *connect;

int main()
{
 connect=mysql_init(NULL);
 mysql_real_connect(connect, NULL, "root", "suvp" ,"Employees" ,0,NULL,0);

 /*Other Code*/

 mysql_free_result(res_set);
 mysql_close(connect);
}

数据库连接的变量是全局的。在我的“其他代码”中,我调用其他函数,这些函数在必要时使用已初始化的connect(在mysql_query等)和row以及res_set。并且看到我释放结果并关闭main的结尾处的连接。

我将res_set(每次都不释放它)从一个函数重用到另一个函数。这会导致问题吗?

在我使用的所有功能中,staments类似

mysql_query(connect,myQuery.c_str())
res_set = mysql_store_result(connect);
row = mysql_fetch_row(res_set);

Valgrind报告此事,

==4864== LEAK SUMMARY:
==4864==    definitely lost: 0 bytes in 0 blocks
==4864==    indirectly lost: 0 bytes in 0 blocks
==4864==      possibly lost: 0 bytes in 0 blocks
==4864==    still reachable: 99,954 bytes in 30 blocks
==4864==         suppressed: 0 bytes in 0 blocks

详细错误指出

mysql_real_connect

从我的主要功能调用。

根据此page调用mysql_library_end()是一种很好的做法。但即使我在关闭连接后拨打mysql_library_end()之后也是如此。 Valgrind说,

==5120== HEAP SUMMARY:
==5120==     in use at exit: 116,466 bytes in 34 blocks
==5120==   total heap usage: 95 allocs, 61 frees, 147,218 bytes allocated

==5120== LEAK SUMMARY:
==5120==    definitely lost: 0 bytes in 0 blocks
==5120==    indirectly lost: 0 bytes in 0 blocks
==5120==      possibly lost: 0 bytes in 0 blocks
==5120==    still reachable: 116,466 bytes in 34 blocks
==5120==         suppressed: 0 bytes in 0 blocks

如前所述,它们都归结为mysql_real_connect

程序运行正常。但是valgrind表示存在问题。我哪里错了?

1 个答案:

答案 0 :(得分:2)

可能没什么。你必须阅读这个。第一个答案非常明确。

TL; DR:仍然可以访问不是真正的“内存泄漏”,但是当程序即将终止的时候,程序中仍然有指针(程序中的指针在程序终止之前没有释放)。 / p>

Still Reachable Leak detected by Valgrind