Serious Problem here... i'm getting ECX_BAD_ACCESS if i try to NSLog an instance variable of my custom object. Following Function is called in my ViewController, payload
holds String Data which is pulled from a url.
- (void) initVcardWithData:(NSString *)payload {
NSLog(@"1. initVcardWithData");
aVCard = [[vcardItem alloc] initWithPayload:payload];
VCardViewController *aVCardViewController = [[VCardViewController alloc] initWithVCard:aVCard];
[self presentModalViewController:aVCardViewController animated:YES];
[aVCard release];
}
So far so good. The initWithWithVCard function is as follows, theVCard
and theVCardN
are defined in @implementation and also set as a @property (nonatomic, retain) in (.h).:
-(id)initWithVCard:(vcardItem *)aVCard {
if(self = [super init]) {
theVCard = [aVCard retain];
theVCardN = [theVCard.PersonName retain];
}
NSLog(@"---- vCardViewController :: initWithVcard :: FirstName: %@", theVCard.PersonName.FirstName);
return self;
}
If i access the theVCardN
object in my ViewController aVCardViewController
within ViewDidLoad
everything works like a charm. I set some labels with data from that object.
If i then try to access the instance variables from theVCardN
within a function which is called from an IBAction which is connected to a button in View, i get an EXC_BAD_ACCESS error at the debugger console. The Function which tries to pull data from the instance variables is as follows:
-(IBAction)addressbookButtonTapped {
NSLog(@"RETAIN COUNT FOR theVCard: %i", [theVCard retainCount]);
NSLog(@"RETAIN COUNT FOR theVCardN: %i", [theVCardN retainCount]);
NSLog(@"Save to Adressbook: %@", theVCardN.FirstName);
//[self dismissModalViewControllerAnimated:YES];
}
The RetainCounter for theVCardN
right before calling NSLog outputs "1". The NSLog Line then returns EXC_BAD_ACCESS in Debugger Console.
Any idea ?
retainCount
returns the absolute retain count of an object. The actual value will be an implementation detail that is very often completely out of your control as the system frameworks may do any number of things internally to cause the retain count to be modified in ways you don't expect.
It is useless for debugging and their are a wealth of tools that are specifically focused on tracking down these kinds of issues.
First, if there is a crash, there is a backtrace. Post it. Probably not that interesting in this case, but, still, always look to the backtrace to at least confirm that it is crashing where/how you think it is.
From the evidence posted, it sounds like theVCardN.FirstName
is either set to garbage or the underlying string has been over-released. Turn on zombie detection mode and see if that is the case. Since it is crashing on FirstName
, then show the code related to creating/storing the FirstName
.
Also, instance variables and methods should always start with a lowercase letter; PersonName
should be personName
& FirstName
should be firstName
.