★ TouchArcade needs your help. Click here to support us on Patreon.

Help with EXC_BAD_ACCESS

10-15-2009, 08:08 PM
#1
Joined: Mar 2009
Location: Los Angeles, CA
Posts: 7
Help with EXC_BAD_ACCESS

Hi All,

I know this is a problem with memory management, but I can't figure out how to solve it.

I've got an object of class A. It's got one NSString instance variable. It's interface is as follows:
@interface mapSymbol : A{
NSString *actionCall;
}
-(void) setActionCall: (NSString *)incoming;
-(NSString *) getActionCall;
and the implementation is
-(void) setActionCall: (NSString *)incoming {
actionCall = incoming;
NSLog(@"Action call set to :%@", actionCall);
}

-(NSString *) getActionCall {
NSLog (@"returning Action Call");
return actionCall;
}
I'm calling the getActionCall method from an object of class B. But, when I try to run any opertion on the returned NSString, I get the EXC_BAD_ACCESS error. Any ideas?

The call from class B is
NSString *tempString = [tempObject getActionCall];
where tempObject is the class A object, stored in an array.

I've been stuck on this for 2 hours, and can't figure it out. Any help would be appreciated.
10-15-2009, 08:17 PM
#2
Joined: May 2009
Location: Palmerston North, New Zealand
Posts: 464
You should really be using properties for this since your getters and setters don't do anything out of the ordinary. So get rid of the functions altogether and declare actionCall as a property then synthesize the getters and setters in your implementation.

You probably need to be using retain for the property rather than assign since the initial value that you assign seems to be getting autoreleased and hence it is no longer in existence when you try and grab it again.

Check out Sheepish, 100+ levels of puzzle fun! With sheep!
And more about us: www.pixelthismobile.com
plus+ id: Newtz
10-15-2009, 08:22 PM
#3
Joined: Feb 2009
Location: San Jose, CA
Posts: 546
Quote:
Originally Posted by PixelthisMike View Post
You should really be using properties for this since your getters and setters don't do anything out of the ordinary. So get rid of the functions altogether and declare actionCall as a property then synthesize the getters and setters in your implementation.

You probably need to be using retain for the property rather than assign since the initial value that you assign seems to be getting autoreleased and hence it is no longer in existence when you try and grab it again.
Exactly what I was going to say. Usually in ClassA, I would have something like:

@property (nonatomic, retain) NSString* actionCall

I also suspect you are not retaining the string, and it's getting auto released.
10-15-2009, 08:22 PM
#4
Joined: May 2009
Location: Palmerston North, New Zealand
Posts: 464
Quote:
Originally Posted by lazypeon View Post
Exactly what I was going to say. Usually in ClassA, I would have something like:

@property (nonatomic, retain) NSString* actionCall

I also suspect you are not retaining the string, and it's getting auto released.
Exactly

It is also worth noting that obj-c strings are objects and not primitives so they have to be memory managed in the same way that all objects do.

Check out Sheepish, 100+ levels of puzzle fun! With sheep!
And more about us: www.pixelthismobile.com
plus+ id: Newtz

Last edited by PixelthisMike; 10-15-2009 at 08:25 PM.