Help with EXC_BAD_ACCESS

Discussion in 'Public Game Developers Forum' started by mikejstein, Oct 16, 2009.

  1. mikejstein

    mikejstein Member

    Mar 23, 2009
    7
    0
    0
    Game Designer, Coder
    Los Angeles, CA
    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.
     
  2. PixelthisMike

    PixelthisMike Well-Known Member

    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.
     
  3. lazypeon

    lazypeon Well-Known Member
    Patreon Bronze

    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.
     
  4. PixelthisMike

    PixelthisMike Well-Known Member

    #4 PixelthisMike, Oct 16, 2009
    Last edited: Oct 16, 2009
    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.
     

Share This Page