views:

8

answers:

1

I have an instance variable declared in the implementation file which can be accessed using the property defined by synthesize

@synthesize myProperty

Now, I want to assign this property something inside the Selector event of the MenuItem in cocos2d library. You can think of it as a accessing myProperty in a callback function. For some reason whenever I access the property it says "property is out of scope". So I tried to assigned the access the self.myProperty which worked!!

But now I have a memory leak in self.myProperty. If I release self.myProperty in dealloc then it throws an exception saying that I also have myProperty release.

UPDATE 1: (Code)

NSString *voice;

@property (nonatomic,retain) NSString *voice; @synthesize voice;

-(void)repeatAlphabet:(id)sender 

{
 *// I cannot access the voice variable in this function.* 

[[SimpleAudioEngine sharedEngine] playEffect:[[voice lowercaseString] stringByAppendingString:@".caf"]]; 
}

-(void) addRepeatButtonOnScreen 

{

CCMenuItemImage * menuItem1 =[CCMenuItemImage itemFromNormalImage:@"image1.png"

selectedImage: @"image2.png"

  target:self

selector:@selector(repeatAlphabet:)];

CCMenu *menu = [CCMenu menuWithItems:menuItem1,nil]; 

menu.position = ccp(30, 450); 

[self addChild:menu]; 


}
A: 

Assuming you have defined an ivar corresponding to your property also called myProperty, and that your property is either retain or copy: in your dealloc you should do [myProperty release]; not [self.myProperty release].

UPDATE: after discussion and more code shown... 'out of scope' can be a symptom of not having retained a value that should be retained. For instance for a string value = @"Value" doesn't retain (and will become out of scope), fixed by using self.value = @"Value" or value = [@"Value" retain].

jv42
Thanks for the reply! Yes I do [myProperty release] but since I am using self.myProperty inside the callback function of the MenuItem selector self.myProperty never gets released and it shows memory leak in the instruments application.
johndoe
You need to be more specific then by what you mean by 'using' (showing code would help a lot). If your property is synthesized and retain, self.myProperty = value automatically release previous value before retaining, so there is no leak.
jv42
Please check the above updated with code! Thanks for yr help!
johndoe
OK and what's the value of voice when there is a problem? How is it setup?
jv42
The value of voice is assigned in a different function. The value usually is just a string like "voice1", "voice2" etc. But when the repeatAlphabet if triggered the variable voice cannot be accessed and it is considered out of scope!
johndoe
Do you setup voice using voice = @"..." or object.voice = @"...", ie using the property or directly the ivar? If the ivar directly, it needs to be retained.
jv42
I just use voice = "something";
johndoe
That's your bug :) You should either use the property's setter (`self.voice = @"something"`, or `yourObject.voice = @"something"` if in another class) or retain the string using `voice = [@something retain]`). Else the value affected to voice will become undefined at a random point (autorelease).
jv42
Thanks I will try it out and see the outcome!
johndoe
I just tried it out and it seemed like it solved the problem! In the dealloc I am still releasing the voice object like this [voice release] will this be okay?
johndoe
That's okay yes, else it would leak.
jv42