有关CCSprite初始化的内存问题

时间:2012-10-24 03:18:49

标签: cocos2d-iphone

我想知道如果我做以下情况,是否考虑泄漏? 如何修改代码以使其看起来更清晰/更好?

// ClassA
@interface ClassA
    CCSprite* image;

@property (nonatomic, retain) CCSprite* image;
@end

@implementation ClassA
@synthesize image;
- (id)init
{
    image = [[CCSprite alloc] init];
}

- (void)dealloc
{
    [image release];
    [super dealloc];
}

@interface Main
@end

@implementation Main
- (id)init
{
     ClassA* aObject = [[ClassA alloc] init];
     aObject.image = [CCSprite spriteWithFile: @"something.png"];
}
@end

1 个答案:

答案 0 :(得分:0)

我使用同样的东西..在你的代码中看到,为图像分配了两次内存。

@interface ClassA : CCSprite
{

}

@end

@implementation ClassA
- (id)init
{

}

- (void)dealloc
{
    printf("Released..\n");
    [super dealloc];
}

@interface Main
@end

@implementation Main
- (id)init
{
     ClassA* aObject =  [ClassA spriteWithFile: @"something.png"];
     [self addChild:actor z:3 ];

}

- (void)dealloc
{
    [self removeAllChildrenWithCleanup:YES];

    [super dealloc];
}
@end