我正在使用MySQL C++连接器。Valgrind在每个连接上泄漏了192个字节。它只在线程环境中泄漏内存,而没有线程,它没有泄漏任何内存。我做错了什么?我是否需要调用其他函数进行清理?样本代码:
#include <pthread.h>
#include <iostream>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
using namespace std;
void* test(void* arg) {
try {
sql::Driver *driver;
sql::Connection *con;
/* Create a connection */
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "root", "root");
/* Connect to the MySQL test database */
con->setSchema("test");
delete con;
} catch (sql::SQLException &e) {
cout << "# ERR: SQLException in " << __FILE__;
cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
return NULL;
}
int main() {
pthread_t thread1, thread2, thread3, thread4;
pthread_create(&thread1, NULL, test, NULL);
pthread_create(&thread2, NULL, test, NULL);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
return 0;
}
Valgrind输出:
==10252== Memcheck, a memory error detector
==10252== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==10252== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==10252== Command: ./app/bin/app-test
==10252== Parent PID: 6312
==10252==
==10252==
==10252== HEAP SUMMARY:
==10252== in use at exit: 384 bytes in 2 blocks
==10252== total heap usage: 212 allocs, 210 frees, 208,400 bytes allocated
==10252==
==10252== 192 bytes in 1 blocks are definitely lost in loss record 1 of 2
==10252== at 0x4C29DB4: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==10252== by 0x5E1CB3E: my_thread_init (in /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18.0.0)
==10252== by 0x5E1CE3C: my_thread_global_init (in /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18.0.0)
==10252== by 0x5E1AA54: my_init (in /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18.0.0)
==10252== by 0x5DF86CA: mysql_server_init (in /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18.0.0)
==10252== by 0x4EA3C08: sql::mysql::NativeAPI::getCApiHandle(sql::SQLString const&) (in /usr/lib/libmysqlcppconn.so.5.1.1.0)
==10252== by 0x4EA40EA: sql::mysql::NativeAPI::MySQL_NativeDriverWrapper::MySQL_NativeDriverWrapper(sql::SQLString const&) (in /usr/lib/libmysqlcppconn.so.5.1.1.0)
==10252== by 0x4EA4138: sql::mysql::NativeAPI::createNativeDriverWrapper(sql::SQLString const&) (in /usr/lib/libmysqlcppconn.so.5.1.1.0)
==10252== by 0x4E6F2F3: sql::mysql::MySQL_Driver::MySQL_Driver(sql::SQLString const&) (in /usr/lib/libmysqlcppconn.so.5.1.1.0)
==10252== by 0x4E6F50B: sql::mysql::get_driver_instance_by_name(char const*) (in /usr/lib/libmysqlcppconn.so.5.1.1.0)
==10252== by 0x40AB8C: test(void*) (main.cc:17)
==10252== by 0x50D9E99: start_thread (pthread_create.c:308)
==10252==
==10252== 192 bytes in 1 blocks are definitely lost in loss record 2 of 2
==10252== at 0x4C29DB4: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==10252== by 0x5E1CB3E: my_thread_init (in /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18.0.0)
==10252== by 0x5DF86DC: mysql_server_init (in /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18.0.0)
==10252== by 0x5DFE85E: mysql_init (in /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18.0.0)
==10252== by 0x4EA4A82: sql::mysql::NativeAPI::MySQL_NativeConnectionWrapper::MySQL_NativeConnectionWrapper(boost::shared_ptr<sql::mysql::NativeAPI::IMySQLCAPI>) (in /usr/lib/libmysqlcppconn.so.5.1.1.0)
==10252== by 0x4EA4014: sql::mysql::NativeAPI::MySQL_NativeDriverWrapper::conn_init() (in /usr/lib/libmysqlcppconn.so.5.1.1.0)
==10252== by 0x4E6F0DC: sql::mysql::MySQL_Driver::connect(sql::SQLString const&, sql::SQLString const&, sql::SQLString const&) (in /usr/lib/libmysqlcppconn.so.5.1.1.0)
==10252== by 0x40ABE8: test(void*) (main.cc:18)
==10252== by 0x50D9E99: start_thread (pthread_create.c:308)
==10252== by 0x5AFCCBC: clone (clone.S:112)
==10252==
==10252== LEAK SUMMARY:
==10252== definitely lost: 384 bytes in 2 blocks
==10252== indirectly lost: 0 bytes in 0 blocks
==10252== possibly lost: 0 bytes in 0 blocks
==10252== still reachable: 0 bytes in 0 blocks
==10252== suppressed: 0 bytes in 0 blocks
==10252==
==10252== For counts of detected and suppressed errors, rerun with: -v
==10252== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 2 from 2)
3条答案
按热度按时间cbjzeqam1#
正如WhozCraig建议的那样,你可以将
delete Driver;
添加到你的测试函数中,但是我建议你使用auto_ptr
或C11unique_ptr
或shared_ptr
来处理所有MYSQL,这样你就永远不必担心内存泄漏了。就拿这个举例吧
C11
或者C++
编辑
你不能只是
delete Driver
,我会看看它更多的时候,我有时间更新
我看了
driver.h
的源代码,确实是protected: virtual ~Driver() {}
,所以你不能只使用删除,怎么在public:
中有两个这也许是你需要的
还有这个example可能是非常有用的,做这一点有点不同,然后你
w1jd8yoj2#
第一个问题是你的代码是错误的。如果在
try
块中出现任何异常,程序将永远不会到达delete con;
语句,并且您将与MySQL服务器和内存泄漏保持开放连接。你必须在try
块外声明并删除con
,或者使用C++11智能指针:错误:
正确,C++11之前:
正确,C++11,推荐方式:
另外,
driver = get_driver_instance();
应该只在程序中调用一次,而不是在每个线程中。因此,您应该将sql::Driver *driver;
设置为全局变量,并在创建任何线程之前在main()
中调用driver = get_driver_instance();
。8ljdwjyq3#
我也收到了Valgrind的警告。您不能更改MySQL++,但可以将问题限制为0或1个泄漏。对我来说,我有一个带有1个线程的类,它使用一个线程安全队列处理所有同步/异步消息的SQL通信。
对于每一条消息,我都有一个包含信号量、回调函数、类型和消息id的类。ID用于日志记录。
而且,所有的重入问题都可以解决,因为不是所有的mysql实现都是线程安全的。
如果人们感兴趣,我会把这段代码放在sourceforge上。它被证明是有效的,因为它在许多荷兰餐馆的Linux POS软件中使用MySQL5.7。我也和Valgrind一起检查过了。