1

我想以某种方式组织我的 iPhone 游戏的关卡视图,但我根本做不到(不展开Object Allocations)。我为我的代码做了一个真正的“骨架”(这个游戏有 2 个级别,目标是发布 iPhone 显示器)。我只是无法释放上一个级别,因此仪器显示递增的 BGTangramLevel 实例。

请看一下,我需要一些有用的设计想法(我的第三个问题)。

视图控制器.h

@interface compactTangramViewController : UIViewController
{
    //The level.
    BGTangramLevel *currentLevel;
    UIColor *levelColor;
}

//It is to be just a reference, therefore I use assign here.
@property (nonatomic, retain) BGTangramLevel *currentLevel;

-(void) notificationHandler: (NSNotification*) notification;
-(void) finishedCurrentLevel;

@end

视图控制器.m

@implementation compactTangramViewController
@synthesize currentLevel;

//Initializer functions, setting up view hierarchy.
-(void) viewDidLoad
{   

    //Set up levelstepper.
    levelColor = [UIColor greenColor];

    //Set up "state" classes.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationHandler:) name:@"finishedCurrentLevel" object:nil];   

    //Attach level 1.
    currentLevel = [BGTangramLevel levelWithColor: levelColor frame:self.view.frame];
    [self.view addSubview:currentLevel];

    [super viewDidLoad];

}

//Release objects.
-(void) dealloc
{
    [currentLevel release];
    [super dealloc];
}

//Notification handling.
-(void) notificationHandler: (NSNotification*) notification
{
    //Execute level swap.
    if ([notification name] == @"finishedCurrentLevel") [self finishedCurrentLevel];
}

-(void) finishedCurrentLevel
{
    //Remove previous level.
    [currentLevel removeFromSuperview];
    //[currentLevel release];

    //Step level.
    if (levelColor == [UIColor greenColor]) levelColor = [UIColor blueColor]; else levelColor = [UIColor greenColor];

    //Attach level 2.
    currentLevel = [BGTangramLevel levelWithColor: levelColor frame:self.view.frame];
    [self.view addSubview:currentLevel];

}
@end

BGTangramLevel.h

@interface BGTangramLevel : UIView
{
    BOOL puzzleCompleted;   
}

//Initializer.
+(BGTangramLevel*)levelWithColor: (UIColor*) color frame: (CGRect) frame;

//Test if the puzzle is completed.
-(void) isSolved;

@end

BGTangramLevel.m

@implementation BGTangramLevel

//Allocated instance.
+(BGTangramLevel*)levelWithColor: (UIColor*) color frame: (CGRect) frame
{
    BGTangramLevel *allocatedLevel = [[BGTangramLevel alloc] initWithFrame:frame];
    allocatedLevel.backgroundColor = color;
    return allocatedLevel;
}

//Finger released.
-(void) touchesEnded: (NSSet*)touches withEvent: (UIEvent*)event
{
    //The completement condition is a simple released tap for now...
    puzzleCompleted = YES;
    [self isSolved];    
}

//Test if the puzzle is completed.
-(void) isSolved
{
    //"Notify" viewController if puzzle has solved.
    if (puzzleCompleted) [[NSNotificationCenter defaultCenter] postNotificationName:@"finishedCurrentLevel" object:nil]; 
}

-(void) dealloc
{
    NSLog(@"Will ever level dealloc invoked."); //It is not.
    [super dealloc];
}

@end

所以我该怎么做?我尝试标记自动释放返回的级别实例,在 removeFromSuperview 之后释放 currentLevel,尝试以(非原子,分配)方式合成的 currentLevel 属性,但对象分配仍然增长。我可以避免通知吗?我被困住了。

4

1 回答 1

3

您需要更严格地遵循保留/释放规则。你绝对不应该为了找到有用的东西而在一些地方实验性地添加保留、释放和自动释放。关于 Cocoa 内存管理的文章已经很多了,这里不再赘述。

具体来说,BGTangramLevel 的levelWithColor:frame:方法应该[allocatedLevel autorelease]在将allocatedLevel 返回给它的调用者之前调用。它不拥有该对象,由调用者保留它。

您还需要了解访问实例变量和访问属性之间的区别。Cocoa 的属性只是 getter 和 setter 方法的语法糖。当您currentLevel在视图控制器中引用时,您正在直接处理实例变量。当您引用时,self.currentLevel您正在处理该属性。

即使您已经声明了一个属性,也currentLevel = [BGTangram ...]只需将引用复制到变量中即可。在中,如果要通过属性的 setter 方法,则viewDidLoad需要使用该方法,该方法将保留对象(因为您以这种方式声明了属性)。self.currentLevel = [BGTangram ...]看到不同?

我认为您的泄漏发生在finishedCurrentLevel. 如果您使用self.currentLevel = [BGTangram ...]了 ,则将调用属性的 setter 方法,该方法将释放旧对象并保留新对象。因为您直接分配给实例变量,所以您只需覆盖对旧级别的引用而不释放它。

调用视图控制器[currentLevel release]dealloc方法是正确的。

于 2009-12-02T01:10:40.780 回答