1

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

// 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
4

1 回答 1

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
于 2012-10-24T03:27:16.820 回答