内存泄漏使用boost::存档::binary_iarchive

Memory Leak using boost::archive::binary_iarchive

本文关键字:存档 binary iarchive boost 泄漏 内存      更新时间:2023-10-16

反序列化正在工作,问题是内存泄漏。 我已经尝试删除"s"指针,但出现"定位失败",我无法删除指针。

//Statment MySql
sql::Statement *_stmt = this->con->createStatement();
sql::ResultSet *_result = _stmt->executeQuery("SELECT * FROM matches ORDER BY `match_seq_num` ASC LIMIT 1250");
while(_result->next()){
std::istream *s = _result->getBlob("match_object");
boost::archive::binary_iarchive ia(*s);
Match _match;
ia >> _match;
delete s;
}
delete _result;
delete _stmt;

问题是在使用boost::archive::binary_iarchive ia(*s);反序列化来自 mysql 的信息后删除"s"指针。

当然,您需要删除。

如果这会带来问题,您应该修复它。您的更新指向一个可能的罪魁祸首:输入存档引用istream并且仍然可以在析构函数中访问它(事实上我认为它很可能确实如此(。因此,请确保在销毁所需资源之前它已消失:

//Statment MySql
sql::Statement *_stmt = this->con->createStatement();
sql::ResultSet *_result = _stmt->executeQuery("SELECT * FROM matches ORDER BY `match_seq_num` ASC LIMIT 1250");
while(_result->next()){
Match _match;
{
std::istream *s = _result->getBlob("match_object");
{
boost::archive::binary_iarchive ia(*s);
ia >> _match;
}
delete s;
}
}
delete _result;
delete _stmt;