我正在用 C++ 编写一个守护进程,它的任务很简单,就是将一些事件插入到 mysql 数据库中。
当我运行 top 命令时,我看到进程的内存需求增加了,我以为我有内存泄漏,我开始使用 Valgrind
我这样运行 valgrind:
valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes ./my_app
我得到以下报告:
==17045== 128 bytes in 1 blocks are definitely lost in loss record 6 of 11
==17045== at 0x402A629: calloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==17045== by 0x40AAB63: my_thread_init (in /usr/lib/i386-linux-gnu/libmysqlclient.so.18.0.0)
==17045== by 0x40AAE43: my_thread_global_init (in /usr/lib/i386-linux-gnu/libmysqlclient.so.18.0.0)
==17045== by 0x40A92D7: my_init (in /usr/lib/i386-linux-gnu/libmysqlclient.so.18.0.0)
==17045== by 0x40863FA: mysql_server_init (in /usr/lib/i386-linux-gnu/libmysqlclient.so.18.0.0)
==17045== by 0x4087B28: mysql_init (in /usr/lib/i386-linux-gnu/libmysqlclient.so.18.0.0)
==17045== by 0x8049890: write_db(std::string, std::string, std::string) (Listener.cpp:76)
==17045== by 0x804A692: SocketListener(void*) (Listener.cpp:182)
==17045== by 0x4052D4B: start_thread (pthread_create.c:308)
==17045== by 0x4582D3D: clone (clone.S:130)
函数 write_db 是这样的:
void write_db(std::string userid,std::string zona,std::string eveniment)
{
try
{
MYSQL * connect;
connect = mysql_init(NULL);
connect = mysql_real_connect(connect,"127.0.0.1","myusr","mypwd","mytbl",0,NULL,0);
std::string stmt = "INSERT INTO t_evenimente(placaid,codev,zona,cand) VALUES(\"" + userid + "\"," + eveniment + ",\"" + zona + "\",NOW())";
std::cout << stmt << std::endl;
mysql_query(connect,stmt.c_str());
mysql_close(connect);
std::cout << "Inserat eveniment obiectiv " << userid << std::endl;
}
catch (...)
{
std::cout <<"Exceptie MYSQL" << std::endl;
}
}
内存泄漏在哪里?我正在使用 mysql_init 并按照文档所述关闭...这可能是误报吗?