PROBLEM
: メモリ リークの原因は何ですか?
SITUATION
: MySQL C API を使用して、MySQL と一緒に C++ を使用して単純なコマンド ライン プログラムを作成しました。
malloc xx bytes"
問題は、 xx が数バイトから 8 kb の範囲のオブジェクトから多くの「マイナーな」メモリ リークがプログラムにあることです。すべてのリークはライブラリにリンクしていますlibmysqlclient.18.dylib
。
それが問題であるかどうかを確認するために、コードからすべてを既に削除しましたmysql_free_result()
が、それでも同じです。
私の MySQL コードは、主に次のような単純なコードで構成されています。
接続する:
MYSQL *databaseConnection()
{
// declarations
MYSQL *connection = mysql_init(NULL);
// connecting to database
if(!mysql_real_connect(connection,SERVER,USER,PASSWORD,DATABASE,0,NULL,0))
{
std::cout << "Connection error: " << mysql_error(connection) << std::endl;
}
return connection;
}
クエリの実行:
MYSQL_RES *getQuery(MYSQL *connection, std::string query)
{
// send the query to the database
if (mysql_query(connection, query.c_str()))
{
std::cout << "MySQL query error: " << mysql_error(connection);
exit(1);
}
return mysql_store_result(connection);
}
クエリの例:
void resetTable(std::string table)
{
MYSQL *connection = databaseConnection();
MYSQL_RES *result;
std::string query = "truncate table " + table;
result = getQuery(connection, query);
mysql_close(connection);
}