私はこのようなコードを持っています:
MyClass.h
@interface MyClass : CCLayer {
}
@property (nonatomic, retain) CCSprite *spriteName; //retain count = 1
@property (nonatomic, retain) CustomClass *customVariable; //retain count = 1
@end
MyClass.m
@implementation MyClass
@synthesize spriteName;
@synthesize customVariable;
//rough init method
-(void)init
{
self.spriteName = [CCSprite spriteWithFileName:@"a.png"]; //retain count = same
self.customVariable = [[CustomClass alloc] init]; //retain count = 2
}
-(void)dealloc
{
[self.spriteName release]; //retain count = 0
self.spriteName = nil;
[self.customVariable release]; //retain count = 1?
self.customVariable = nil;
[super dealloc];
}
これについていくつか質問があります:
1)プロパティに作成したCCSpriteがありますが、これをに割り当てると[CCSprite spriteWithFileName:@"a.png"]
、自動解放されたオブジェクトになります。しかし、私が持っているので@property (nonatomic, retain) CCSprite *spriteName
、私はそれを解放しなければなりませんか?
2)customVariableの場合、上記の方法でリリースするとエラーが発生するようですが、アンダースコアに変更して[_customVariable release]; _customVariable = nil
もエラーは発生しません。これはなぜですか、2つの違いは何ですか?
3)これらのオブジェクトを正しくリリースしていますか?理解に苦労していると思う保持数にコメントしました。割り当てまたは保持があるかどうかの基本を知っているので、+ 1にする必要がありますが、プロパティに関しては混乱しています。