views:

40

answers:

3

Hi there, i got this piece of code:

- (void)postToWall {

FBStreamDialog *dialog = [[FBStreamDialog alloc] init];
dialog.userMessagePrompt = @"Un tuo commento:";
dialog.attachment = [NSString stringWithFormat:@"{\"name\":\"Bla: %@\"", facebookName];
[dialog show];

[dialog release];
}

the first time it get executed it works fine, no problem. But if I post or skip and then I post again I got an EXC_BAD_ACCESS, due to facebookName. The console shows no error, I found it via DebugConsole. I really don't know why this happens, can someone help?

EDIT: SOLVED!!! In other parts of the code, I accessed the facebookName string by its name. This apparently leads to crash, so I synthesized it and then accessed it by "self.facebookName".

Thank you.

A: 

type backtrace when you get this exception @ runtime environment on XCODE.

the issue is clear something that has been released is being used. double check it.

Try debugging with this

org.life.java
Thanks, already solved. But if I don't need to access the ivar outside this object, I should not need to synthesize it... what is the fact behind property/synthesize? Isn't it a simple way to creat get/set method for that ivar?
IssamTP
@IssamTP: Yes, it creates getters and setters for the variable. But getters and setters are not just for accessing a property outside an object — they're for making sure memory is managed correctly. If you directly set an instance variable, you have to remember to release its old value and retain or copy the new one if appropriate. Do it wrong somewhere and you get a crash like this or a memory leak. A setter encapsulates most of that rigamarole.
Chuck
+1  A: 

You should show contextual code regarding facebookName.

I think maybe it is being released by the time you use it again. Just to be safe, you can try doing [facebookName retain] at the beginning of the method and then [facebookName release] at the end, to signify that you need to hold on to the object to do some work.

Yep, using the synthesized property automatically retains objects when you assign them (provided you have the usual, (nonatomic, retain)). Before, it wasn't retaining so by the time you used it again a couple times, you would get EXC_BAD_ACCESS since it no longer existed (was released by then, cause again, it wasn't retained).

Jorge Israel Peña
So, I guess that the best way to manage ivars is to use property/synthesize couple, isn't it?
IssamTP
Well, it's usually what you want. Once you get the hang of memory management in objective-c you'll know in what situations to do different.
Jorge Israel Peña
A: 

You will likely not get a valid stack trace at the time of the crash.

The quickest way to track down the real cause of EXC_BAD_ACCESS is by using the NSZombieEnabled executable argument, and then setting a breakpoint on objc_exception_throw. This will get you a stack trace, and allow you to determine specifically which object you are trying to access.

http://www.cocoadev.com/index.pl?NSZombieEnabled

Using Malloc to debug

Jerry Jones