mysqlcppconnen程序内存泄漏
memory leak in mysqlcppconn program
我有一个运行的程序占用越来越多的内存。这种情况一直持续到整个程序崩溃。我已经把它缩小到这一部分——如果我把它注释掉,使用的内存就不会再增加了。
为什么这段代码会导致内存泄漏?指针是否由于某种原因没有被删除?我是否使用了错误的executeUpdate
函数?
#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 problemn";
}
// free
delete pstatement;
您正在创建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 problemn";
}
相关文章:
- C++功能泄漏内存,我是C++新手,不确定如何解决
- 我的堆栈弹出式磁带的实现是否泄漏内存?
- 将 c++ 向量转换为字符 ** 而不会泄漏内存
- 析构函数 C++ 使泄漏内存
- 构造函数对象赋值是否泄漏内存
- corba :: orb_init泄漏内存
- Gmock泄漏内存
- 如何在不泄漏内存的情况下删除链接列表
- Visual C ODBC关闭记录集泄漏内存
- 为什么泄漏内存比在动态数组上执行 delete[] 慢
- OpenGL正在泄漏内存.哪个对象未释放
- 可以std ::退出泄漏内存
- uiautomation findall泄漏内存
- 为什么在此OpenCL代码中泄漏内存,为什么要泄漏内存
- pthread在完成后会泄漏内存
- win32 标准::线程泄漏内存
- 返回指向同一变量的指针是否会泄漏内存
- 使用clectType(new any_type())可能会泄漏内存泄漏
- Windows开发:如何确定我的应用程序是否正在泄漏内存
- WinHttp打开泄漏内存