Question

I have following code, which executes on button press. At first it works as expected but second time onwards application hangs and I get EXC_BAD_ACCESS signal.

- (IBAction) comicDetailsPressed:(id)sender {
   static IssueProperties *props = nil;
    if (props == nil) {
        props = [ComicDataParser 
         parseComicForUrl:@"http://dummy.com/Jan.xml"];
    }

    NSLog(@"%d", [props totalPages]);

    totalPages.text = [NSString stringWithFormat:@"%d", [props totalPages]]; 
}
Was it helpful?

Solution

You didn't say what line it's crashing on, which will mean answers will have to be speculative.

You have a static pointer to a IssueProperties object, but when you assign to it, you aren't using retain. You probably should.

This is assuming that the return value from parseComicForUrl: is a IssueProperties object or a subclass.

I'm assuming that the text property is an NSString set to copy and not retain. If not, it should be.

OTHER TIPS

You need to retain the object you get back from +parseComicForUrl:. Also, why don't you use an instance variable for props?

Without a lot more context, it is going to be impossible to answer for sure, but my first thought would be this:

your static IssueProperties *props would not be nil the second time around. Instead, it would have the value that [ComicDataParser parseComicForUrl] returned.

My guess is that the ComicDataParser is autoreleaseing the response, and so the second time around you have a pointer that is not nil, but is now pointing to an already released object, which is invalid.

If I am right, you need a retain somewhere.

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