【问题标题】:Memory leak in MySQL C++ ConnectorMySQL C++ 连接器中的内存泄漏
【发布时间】:2012-10-26 07:10:34
【问题描述】:

我正在使用 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)

【问题讨论】:

  • 不知道 mysql,valgrind 与您的代码相关的转储表明泄漏是驱动程序实例,但有可能这被隐藏在 TLS 中并保留用于缓存。我很想知道循环中的代码会发生什么(即每个线程 10 次调用),这对您来说测试起来很简单。
  • @WhozCraig 如何释放 driver_ 内存。它的析构函数是私有的吗?
  • @VivekGoel 我面临同样的问题。您能否详细说明您是如何解决此问题的?
  • 除了你的程序会在 try...catch 块中发生异常时发生泄漏,driver 用法是错误的。您需要为驱动程序提供一个全局变量,并且只从main() 调用一次get_driver_instance()。不要在driver 上调用delete,也不要试图以某种方式释放它,它是一个嵌入式结构,应该在你的程序退出之前一直存在。使用线程时,在每个新线程中您必须调用driver-&gt;threadInit();之后您的线程使用完 MySQL 并且之前您的线程终止您必须调用driver-&gt;threadEnd();

标签: c++ mysql mysql-connector


【解决方案1】:

正如 WhozCraig 建议的那样,您可以将 delete Driver; 添加到您的测试函数中,但我建议对所有 MYSQL 使用 auto_ptr 或 C++11 unique_ptrshared_ptr,您将永远不必担心内存泄漏

以这个为例

使用 C++11

std::unique_ptr< sql::Connection > con( driver->connect("tcp://127.0.0.1:3306", "root", "root"));

或 C++

std::auto_ptr< sql::Connection > con( driver->connect("tcp://127.0.0.1:3306", "root", "root"));

编辑

你不能只是delete Driver,有时间我会多研究的

更新

我查看了driver.h源代码,确实是protected: virtual ~Driver() {}所以你不能只使用删除,但是public:有两个

virtual void threadInit() = 0;

    virtual void threadEnd() = 0;

这可能是你需要的

还有这个example,它可能非常有用,而且做起来和你有点不同

【讨论】:

  • 如果我尝试删除驱动器对象,我会收到以下错误。虚拟 sql::Driver::~Driver()' 受保护
  • 我做了一些挖掘,我们在生产中使用它,在我们的代码中没有 delete driver,但在文档和我们的代码中,delete con 之前有一个 con -&gt; close();
  • 如果你只是为驱动添加一个解构器就可以解决问题
  • 我面临同样的问题,但无法获得您提供的解决方案。如何为驱动程序添加解构函数有点令人困惑?您的意思是从 Driver 派生另一个类还是如何?
【解决方案2】:

第一个问题是您的代码错误。如果try 块中出现任何异常,程序将永远不会到达delete con; 语句,您将面临与 MySQL 服务器的开放连接和内存泄漏。您必须在 try 块之外声明和删除 con 或使用 C++11 智能指针:

错误:

  try {
    sql::Connection *con = driver->connect("URL", "user", "password");

    // code

    delete con;
  }
  catch (const sql::SQLException &e) {
    // code
  }

正确,在 C++11 之前:

  sql::Connection *con = NULL;     
  try {
    con = driver->connect("URL", "user", "password");

    // code

  }
  catch (const sql::SQLException &e) {
    // code
  }
  delete con; // delete on NULL is harmless.
  con = NULL;

正确,C++11,推荐方式:

  try {
    // unique_ptr will automatically call delete when it goes out of scope.
    // That will also happen in case of exception.
    std::unique_ptr<sql::Connection> con (driver->connect("URL", "user", "password"));     

    // code

  }
  catch (const sql::SQLException &e) {
    // code
  }

另外,driver = get_driver_instance(); 应该只在您的程序中调用一次,而不是在每个线程中。所以你应该在创建任何线程之前将sql::Driver *driver; 设为全局变量并在main() 中调用driver = get_driver_instance();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-01
    相关资源
    最近更新 更多