views:

47

answers:

3

I have following code, which executes on button press. At first it works as expected but second time onwards application hangs and I get EXE_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]]; 
}

as beginner Mac programmer I'm not able to figure out the problem, any help/suggetion is appreciated.

+1  A: 

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

Graham Lee
making props as an instance variable is good idea, I was just trying some XML parsing thing in objective-c, so I did not think of it. Thanks
Prashant
+2  A: 

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.

Shaggy Frog
Your statement about the `text` property seems like overengineering. While you should copy string properties that could come from any client code, in this case we can only see the property being set by `+stringWithFormat:`, which returns an immutable string. Therefore copy is overkill unless you know something about Prashant's code that we don't.
Graham Lee
@Graham Lee I don't understand how you see it as overengineering; you have to choose either `assign`, `retain` or `copy` for a property, and it's well-known the issues you get with `NSString` properties when you don't use `copy`... so why not avoid future issues and reduce risk if you can?
Shaggy Frog
@Shaggy Frog: YAGNI.
Graham Lee
@Graham Lee huh?
Shaggy Frog
@Shaggy Frog: Ya Aint Gonna Need It. If you try to avoid all the future issues you can think of, you'll never ship 1.0.
Graham Lee
@Graham Lee You're overstating your case about overengineering. As if mentioning the best practice of using `copy` with `NSString` properties is akin to telling the guy he'll never ship his app. Let's dial back the hyperbole a bit.
Shaggy Frog
+1  A: 

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.

Andrew Shelansky
Thank you for quick response, problem is fixed after retaining `props`. I think I should go through Memory management guide again.
Prashant