SqlConnection的executeAndWait导致BB10上的内存泄漏

SqlConnection's executeAndWait causes memory leak on BB10

本文关键字:内存 泄漏 导致 executeAndWait SqlConnection BB10      更新时间:2023-10-16

当我调用executeAndWait并且返回回复时,我看到堆内存增加了76KB。我不知道为什么会这样。如何清理此内存?我与 db 的连接创建如下,

bool DBHelper::checkConnection(bool isAsynch)
{
    if(sqlConnector && dbFile->exists())
    {
        return true;
    }
    if (dbFile->exists())
    {
    sqlConnector = new SqlConnection(dbPath, "connect");
    connect(sqlConnector, SIGNAL(reply(const bb::data::DataAccessReply&)), this,
            SLOT(onLoadAsyncResultData(const bb::data::DataAccessReply&)));
    return true;
    }    
    return false;
}

对 executeAndWait 的调用在此函数中,

void DBHelper::execute (const QVariant &criteria,int id,bool isAsynch)
{
    if (checkConnection(isAsynch))
    {
        if(!isAsynch)
        {
            DataAccessReply reply= sqlConnector->executeAndWait(criteria, id); // memory leak happens when the reply is found.
        this->onLoadSynchResultData(reply);
        }
    }
}

文档链接在这里。

谢谢。

你确定这是内存泄漏而不是DataAccessReply类的一些内部机制吗?您是否尝试过用valgrind或类似工具将其刺穿?

从您使用new分配和后续分配中,我认为sqlConnector的类型是指向某物的指针。虽然它可能不是您正在寻找的解决方案,但我建议使用一些智能指针类型,因为这些总是更防漏。

如果有兴趣,请参阅例如 boost::shared_ptr或C++11 std::shared_ptr取决于您可用的内容。

另外(在这里叫我迂腐)我不会使用if(sqlConnector)即使它可能通过隐式强制转换来达到您的期望。我会显式使用if(sqlConnector != NULL)(或类似的东西)并仔细检查sqlConnector是否正确初始化(也显式地)。