mysqlcppconn程序中的内存泄漏

时间:2019-11-08 15:00:22

标签: c++ mysql

我有一个正在运行的程序,该程序占用越来越多的内存。这一直持续到整个程序崩溃为止。我将其范围缩小到本节-如果我将其注释掉,则所使用的内存不再增加。

这部分代码为什么会给我带来内存泄漏?是否由于某种原因未删除指针?我使用了错误的let testStr = 'man i need a taxi up to ubud' const values = [...Array(27).keys()] values.shift() const keys = String.fromCharCode(...[...Array('z'.charCodeAt(0) - 'a'.charCodeAt(0) + 1).keys()] .map(i => i + 'a'.charCodeAt(0))) const merged = [...keys].reduce((obj, key, index) => ({ ...obj, [key]: values[index] }), {}) console.log(JSON.stringify(merged) + '\n' + testStr) const result = testStr.split(' ') .reduce((acc, cur) => [ ...acc, { name: cur, value: [...cur].reduce((sum, c) => sum + merged[c], 0) } ], []) .reduce((prev, current) => (prev.value > current.value) ? prev : current) .name; console.log(result);函数吗?

executeUpdate

1 个答案:

答案 0 :(得分:3)

您正在创建sql语句N次,但只有最后一个被删除。

删除每个语句会更好:

#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";
}