I’m having memory leak / deleting errors when using MySQL Connector C++ 1.05.
The Connector returns a pointer to a result set from executing query.
I am assigning the pointer to a boost::shared_ptr. The call looks like:
std::string query_text;
query_text = /* ... */;
boost::shared_ptr<sql::Statement> query(p_db_connection->createStatement());
if (!query)
{
return;
}
boost::shared_ptr<sql::ResultSet> query_results(query->executeQuery(query_text));
if (!query_results->next())
{
return;
}
Here are my questions:
- Who is responsible for deleting the
allocated result set? - Should I be using
scoped_ptror
shared_ptrif the results are only
used within the function? - Is the result valid when another
query is executed?
I’m using MySQL Connector C++ 1.05, MS Visual Studio 2008 version 9.0.
1) According to this example, you’re doing everything correct.
If you’re using the
shared_ptr<X>to store the result, it would be automatically disposed after yourshared_ptrobject goes out of the scope (in your case) / has no more actual references (speaking globally).2) It depends, but the most common practice is to use the
scoped_ptr, because it’s contruction and memory deallocation is much faster and using it explicitly states, that the object is valid for the current scope only.3) I am not sure I get the question correctly, but you could do
.resetaction for yourResultsand fill it with the new query result.Also, I’m sure that your leaks are from memory allocated somewhere else (could be in the library too). You might not be deleting something connector-related, see the docs.