【问题标题】:Xerces-c: XMLString::transcode puzzleXerces-c: XMLString::transcode 谜题
【发布时间】:2013-11-09 17:07:49
【问题描述】:

我有这段代码:

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 的释放——如果我不这样做,代码会立即在删除坏指针时出现 seg 错误。但是由于 threadID 是 XMLString::transcode 的结果,它肯定应该被释放吗?

【问题讨论】:

    标签: c++ xml sax xerces-c


    【解决方案1】:

    问题出在线路上——

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

    更新threadID的值

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

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

    解决问题。 (感谢 Alberto Massari 的回答)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多