Prashant Cholachagudda
Prashant Cholachagudda

Reputation: 13090

Why do I get EXC_BAD_ACCESS?

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

Upvotes: 0

Views: 229

Answers (3)

Andrew Shelansky
Andrew Shelansky

Reputation: 5062

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.

Upvotes: 1

Shaggy Frog
Shaggy Frog

Reputation: 27601

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.

Upvotes: 2

user23743
user23743

Reputation:

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

Upvotes: 1

Related Questions