我有这段代码:
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的结果,所以它肯定会被释放吗?
答案 0 :(得分:0)
问题在于行 -
long int tid = strtol(threadID, &threadID, 16); //hex
更新threadID
因此,当delete
尝试发生时,它是一个错误的指针(即它不再指向堆上的正确位置)。
long int tid = strtol(threadID, NULL, 16); //hex
解决了这个问题。 (感谢Alberto Massari的回答)。