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

时间:2017-08-08 01:54:57

标签: c++ c++11 boost memory-leaks

反序列化工作正常,问题是内存泄漏。 我已经尝试删除's'指针,但是有一个'Targeting failure',我无法删除指针。

//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'指针。

1 个答案:

答案 0 :(得分:1)

当然你需要删除。

如果这有问题,你应该解决这个问题。您的更新指出了一个可能的罪魁祸首:输入存档将引用带到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;