1

I am using g++ and mudflap. When I run my code, it aborts where mudflap complaining about a double free:

*** glibc detected *** /mydir/MYMain: double free or corruption (out): 0x0000000002127880 ***

If I run it in gdb, I can see where it aborts:

queue<string> logQueue;
void someThreadSpawnFunction() {
    string str;
    while(true) {
       {
           boost::scoped_lock sl( aMutex);
           // thread wait for loqQueue to have item
           str = logQueue.front();    // <---------------    Aborts here.
           logQueue.pop();
       }
       //write out string
}

The question is: Am I using std::queue and strings incorrectly?

In other parts of the code, there will be something like:

string msg;
// processMessage            -- some bit a of code with stripNull()
logQueue.push(msg);

The only funny thing I can find in processMessage is this function:

std::string Log::stripNull(const std::string &str, bool &gotNulls)
{
    std::string retStr = str;

    // strip any trailing NULLs
    size_t rsltPos = retStr.find_first_of('\0', 0);
    if (rsltPos != string::npos)
    {
        gotNulls = true;
        retStr.resize(rsltPos);
    }
    return(retStr);
}

Is it possible mudflap may be complaining about the stripNull function?

Thanks.

4

1 回答 1

3

您忘记检查是否确实有要获取的字符串。

当队列为空并且您执行.front()时,如果您不走运,您可能会碰巧得到一种“幽灵”版本,该版本曾经是队列中的第一个元素。

然后,当您尝试.pop()使用不存在的对象时,您会间接调用该“幽灵”上的析构函数。原始对象已经被销毁,因此是双重释放的。

于 2013-07-04T16:33:39.387 回答