views:

113

answers:

4

Hi everyone

I use NSXMLParser for parsing an XML document. I have the following functions (among others):

- (void) parserDidStartDocument:(NSXMLParser *)parser {

    // Init tempString
    tempString = [NSMutableString string];

}    
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

        // save gained data for element "date"
        if ([elementName isEqualToString:@"date"])
            [entryDict setObject:[tempString copy] forKey:kXMLDictDateKey];

        [tempString setString:@""];
    }


    //
    // Character Handling
    //
    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
        [tempString appendString:[[XMLParser alloc] stripUnwantedStringChars:string]]; //Just strips tabs and linebreaks and the returns the string
    }

tempString is an instance variable with the following property:

@property (nonatomic, retain) NSMutableString *tempString;

tempString does not have to be released in dealloc as it is initiated with a convenience method, thus it is automatically assigned to an autorelease pool. I have also tried the following with an alloc, init approach, yet with the same result. So here is what I did:

1.) Run my project with instruments, let it search for leaks right after startup, there are none. 2.) run the XML parser once, check for leaks. There are none. 3.) run the XML Parser again, now suddenly the line with [entryDict setObject:[tempString copy] forKey:kXMLDictDateKey]; leaked.

I have been looking into these memory leaks for hours now, what did I forget? If you need more code please let me know, although I think my problem is somewhere in these lines.

Ps. My checks show that between the parser (delegate) calls the "dealloc" method gets called, thus I think the parser is really loaded two times, not just once.

A: 

First of all, I would imagine this should actually crash, since the autoreleased mutable string should be released shortly after you return from parserDidStartDocument. That it doesn't is kind of concerning, and you are also lying in your property definition by claiming the property is retained when it has not been.

However, what Leaks is telling you is that that the copy of the string has been leaked - Leaks shows you where the object leaking was allocated, but that's not the cause of the leak. The cause of the leak is the line of code you don't have that should be releasing that string properly later. Since Leaks can't point to code that does not exist, all it can show you is what made the object that is not being freed correctly.

In this case, I think what you are missing is that an array should hold objects that are autoreleased - so you want to say:

[entryDict setObject:[[tempString copy] autorelease] forKey:kXMLDictDateKey];

Because Copy also retains the copy (just like alloc/init would).

Kendall Helmstetter Gelner
Thank you very much for your quick answer. The practice of attaching tempString to autorelease was copied from Apple's sample code at the "XML Performance" project. I have added the autorelease you suggested, yet I still get the leaks. Why tempString does not get properly deallocated is a mystery to me (I suspect this is the problem?). I also made sure that "entryDict" gets released, which it does, so I think the problem really lies within tempString. Do you have any other idea what could be causing the leak? Thanks a lot for your effort!
Robin
What releases entryDict? If that's not being released then I can see where it would say the elements within are leaking.
Kendall Helmstetter Gelner
+2  A: 

Your call to:

tempString = [NSMutableString string];

Actually does not invoke the property (wrapper) and the retain.

You should do this instead:

self.tempString = [NSMutableString string];

Otherwise you are just setting the ivar directly to an autoreleased object.

Not only do you have a leak somewhere, the above code will at one point result in some interesting crash.

St3fan
Thanks a lot, this was the thing I was missing. Now it works like a charm :)
Robin
+1  A: 

The other bug in your code is:

[tempString appendString:[[XMLParser alloc]
    stripUnwantedStringChars:string]];

This allocates a new XMLParser and never gets rid of it.

St3fan
Yes you are right, I forgot to write in the description that XMLParser is a singleton. Thus this does not create a new instance of XMLParser but just return the previous one.
Robin
A better way to do that is to follow Apple's style and use a `sharedInstance` class method. Messing with `alloc` to create a singleton is evil and really bad style imo.
St3fan
A: 

I'm trying to figure out what you ended up doing in your assignment to tempString. If you're doing this:

self.tempString = [NSMutableString string];

then you do have to release tempString in dealloc. Even though it's being autoreleased, the setter is retaining it.

skochan