Taylor
Taylor

Reputation: 2085

memory leak in mysqlcppconn program

I have a program that runs that takes an increasing amount of memory. This continues until the whole program crashes. I have narrowed it down to this section--if I comment it out, the memory being used doesn't increase anymore.

Why is this section of code giving me a memory leak? Is the pointer not being deleted for some reason? Am I using the wrong executeUpdate function?

#include <cppconn/prepared_statement.h> // preparedStatement
sql::PreparedStatement* pstatement; 
try{
for(const auto& bar : m_bars) {

    std::string sql = "INSERT INTO " 
                    + m_table_name 
                    + " VALUES (' "
                    + trade_platform::datetime::toString(datetime) + "', '"
                    + bar.first + "', "
                    + "'IB', "
                    + std::to_string(bar.second.getOpen()) + ", "
                    + std::to_string(bar.second.getHigh()) + ", "
                    + std::to_string(bar.second.getLow())  + ", "
                    + std::to_string(bar.second.getClose()) + ", "
                    + std::to_string(bar.second.getVolume()) + ");";

    pstatement = m_conn->prepareStatement(sql);
    // prepare our statement and execute query
    pstatement->executeUpdate();
}
}catch(const std::exception& e){
    std::cerr << "flushToDB problem: " << e.what() << "\n"; 
}catch(...){
    std::cerr << "unspecified flushToDB problem\n";
}

// free
delete pstatement;

Upvotes: 1

Views: 183

Answers (1)

Renat
Renat

Reputation: 9002

You are creating the sql statement N times, but only the last one gets deleted.

Deleting each statement would be better:

#include <memory>
...

try{
for(const auto& bar : m_bars) {

    std::string sql = "INSERT INTO " 
                    + m_table_name 
                    + " VALUES (' "
                    + trade_platform::datetime::toString(datetime) + "', '"
                    + bar.first + "', "
                    + "'IB', "
                    + std::to_string(bar.second.getOpen()) + ", "
                    + std::to_string(bar.second.getHigh()) + ", "
                    + std::to_string(bar.second.getLow())  + ", "
                    + std::to_string(bar.second.getClose()) + ", "
                    + std::to_string(bar.second.getVolume()) + ");";

    std::unique_ptr<sql::PreparedStatement> pstatement(m_conn->prepareStatement(sql)); // enabling RAII
    // prepare our statement and execute query
    pstatement->executeUpdate();

    // unique_ptr wil automatically call delete on sql statement
    // after pstatement leaves the scope, and c++ guarantees that 
    // the destructor of pstatement will be called always even in case of exception thrown
}
}catch(const std::exception& e){
    std::cerr << "flushToDB problem: " << e.what() << "\n"; 
}catch(...){
    std::cerr << "unspecified flushToDB problem\n";
}

Upvotes: 3

Related Questions