2013-11-09 34 views
0

我有这样一段代码:的Xerces-C:了XmlString ::转码拼图

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); 
} 

是我百思不得其解的事情是注释掉线程ID发布的需要 - 如果我不代码立即对错误指针进行seg错误删除。但是,由于threadID是XMLString :: transcode的结果,它当然应该被释放?

回答

0

的问题是线 -

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

哪些更新的threadID

的价值。因此,当试图delete它发生,这是一个错误的指针(即它不再指着在堆上的正确位置)。

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

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