I get an ‘EXC_BAD_ACCESS‘ error when trying to access variable in a function other than the one it was set in
The variable is set in the ‘awakeFromNib‘ function:
//Retrieve Session-ID
sessionID = [self getSessionID];
And accessed in ‘searchBtnClick‘:
NSLog(@"Commening search (%@)",sessionID); // This causes the error
The variable itself is defined in the header:
NSString *sessionID;
Can someone suggest what might be wrong with that?
The part which of getSessionID which returns the value:
NSString *pC = @"";
// Separate Session ID
pC = [initCookie substringFromIndex:10];
pC = [pC substringToIndex:32];
NSLog(@"Got session ID : %@",pC);
return pC;
Your
-getSessionIDmethod is returning an autoreleased variable—when you try to access the pointer again later, the string’s already been deallocated and so the reference is no longer valid. You need to call-retainon the variable when you first retrieve it, like this:Then, later, in your class’s
-dealloc, you need to balance the retain with a release: