Search code examples
ioscocos2d-iphone

NSString value nil inside onEnterTransitionDidFinish method


I guess its a very basic memory concept. But couldn't figure it out what happens with below case. Any insight would be helpful.

This could be similar to Problems with NSString inside viewWillDisappear

But I wanted to know why there requires a @property. How can we do it without taking @property. Please provide some inside view.

in .h I have NSString *someString

in .mm (this is my non-ARC cocos2d+box2d game scene)

-(id)initWithString:(NSString *)tempString
{
   if(self = [super init])
   {
       someString = [[NSString allo]init];
       someString = tempString;
   }
return self;
}

-(void)onEnterTransitionDidfinish
{
   [super onEnterTransitionDidfinish];
   NSLog("The String is %@",someString);//Becomes nil here
}

-(void)printString
{
    NSLog(@"The String is %@",someString);//This works fine
}

Solution

  • If you are not using ARC then you need to learn a lot more about memory management.

    The following two lines:

    someString = [[NSString allo]init];
    someString = tempString;
    

    should be:

    someString = [tempString copy]; // or [tempString retain];
    

    And be sure you call [someString release] in your dealloc method.

    BTW - you are not using a property. someString is declared as an instance variable, not a property.