0

我有这段代码:

void startElement(const XMLCh* const uri, const XMLCh* const localname,
    const XMLCh* const qname, const Attributes& attrs) {
    char* temp = XMLString::transcode(localname);
    if (strcmp(temp, "thread") == 0) {
        char* threadID = XMLString::transcode(
            attrs.getValue(emptyStr, tidStr));
        long int tid = strtol(threadID, &threadID, 16); //hex
        if (tid != current) {
            current = tid;
            cout << "Now made " << ++switches 
                << " thread switches and in thread ";
            cout << current;
            if (!(threadbitmap & 1 << tid - 1)) {
                count++;
                threadbitmap = threadbitmap |
                    1 << tid - 1;
            }
            cout << " of " << count << " threads." << endl;
        }
        //XMLString::release(&threadID);
    }
    XMLString::release(&temp);
} 

令我困惑的是需要注释掉 threadID 的释放——如果我不立即删除坏指针时代码会出现段错误。但是由于 threadID 是 XMLString::transcode 的结果,它肯定应该被释放吗?

4

1 回答 1

0

问题是线路 -

   long int tid = strtol(threadID, &threadID, 16); //hex

哪个更新了threadID

因此,当尝试delete它时,它是一个错误的指针(即它不再指向堆上的正确位置)。

   long int tid = strtol(threadID, NULL, 16); //hex

修复问题。(感谢 Alberto Massari 的回答)。

于 2013-11-10T20:52:32.183 回答