Question

I have this piece of code:

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

The thing that is puzzling me is the need to comment out the release of threadID - if I don't the code immediately seg faults on the delete of a bad pointer. But as threadID is the result of a XMLString::transcode, surely it should be released?

Was it helpful?

Solution

The problem is the line -

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

Which updates the value of threadID

Thus when an attempt to delete it takes place, it is a bad pointer (ie it is no longer pointing at the right place on the heap).

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

Fixes the problem. (Thanks to Alberto Massari for the answer).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top