2011-03-04 57 views
0

我瞭解一些這方面的材料,那我就寫了一個簡單 應用程序,但糊塗了,這裏是(使用cocos2d的模板)樣本:內存管理上的NSMutableArray

MySpriteObject類:

#import <Foundation/Foundation.h> 
#import "cocos2d.h" 

@interface MySpriteObject : CCNode { 
    CCSprite *mySprite; 
    NSString *spriteInfo; 
} 

@property(nonatomic,retain) CCSprite *mySprite; 
@property(nonatomic,retain) NSString *spriteInfo; 
@end 


#import "MySpriteObject.h" 

@implementation MySpriteObject 
@synthesize mySprite; 
@synthesize spriteInfo; 

-(id) init 
{ 
    NSLog(@"MySpriteObject init"); 
    if ((self = [super init])) { 

     self.mySprite = [CCSprite spriteWithFile:@"Icon.png"]; 
     self.spriteInfo = [[[NSString alloc] initWithFormat:@"sprite info"] autorelease]; 
    } 

    return (self); 
} 

-(void)dealloc 
{ 
    NSLog(@"MySpriteObject dealloc"); 
    [self.spriteInfo release]; 
    [super dealloc]; 
} 

@end 

MyObjectManager類:

#import "cocos2d.h" 
#import "MySpriteObject.h" 

@class MySpriteObject; 
@interface MyObjectManager : CCNode { 

} 

+(MyObjectManager *)sharedMyObjectManager; 
-(NSMutableArray *)func1; 
-(NSMutableArray *)func2; 

@end 


#import "MyObjectManager.h" 
#import "MySpriteObject.h" 

@implementation MyObjectManager 

static MyObjectManager *_sharedMyObjectManager = nil; 

+(MyObjectManager *)sharedMyObjectManager 
{ 
    NSLog(@"MyObjectManager sharedMyObjectManager"); 
    if (!_sharedMyObjectManager) { 

     if([ [MyObjectManager class] isEqual:[self class]]) 
      _sharedMyObjectManager = [[MyObjectManager alloc] init]; 
     else 
      _sharedMyObjectManager = [[self alloc] init]; 
    } 

    return _sharedMyObjectManager; 
} 

-(id)init 
{ 
    NSLog(@"MyObjectManager init"); 
    if((self = [super init])) { 
    } 
    return self; 
} 

-(void)dealloc 
{ 
    NSLog(@"MyObjectManager dealloc"); 
    [super dealloc]; 
} 

-(NSMutableArray *)func1 
{ 
    NSMutableArray *array1 = [[NSMutableArray alloc] init]; 
    array1 = [self func2]; 
    return array1; 
} 

-(NSMutableArray *)func2; 
{ 
    NSMutableArray *array2 = [[NSMutableArray alloc] init]; 
    for (int i = 0; i < 3; i++) { 

     MySpriteObject *mySpriteObject = [[MySpriteObject alloc] init]; 
     [array2 addObject:mySpriteObject]; 

     //[mySpriteObject release]; 
    } 
    return array2; 
} 
@end 

並在 「HelloWorldScene.m中」 init方法:

-(id) init 
{ 
    if((self=[super init])) { 

     NSMutableArray *array = [[NSMutableArray alloc] init]; 
     array = [[MyObjectManager sharedMyObjectManager] func1]; 
     for (int i = 0; i < array.count; i++) { 
      MySpriteObject *s = [[MySpriteObject alloc] init]; 
      s = [array objectAtIndex:i]; 
      NSLog(@"%@", s.spriteInfo); 
      [s release]; 
     } 
     [array release]; 
    } 
    return self; 
} 

上面貼的代碼工作正常(至少沒有崩潰),但我認爲有 內存泄漏那裏。

起初,我有FUN1 FUN2方法返回數組是這樣的:

return [array1 autorelease]; 
return [array2 autorelease]; 

而且在methoed正如你所看到的註釋行的FUNC2發佈SpriteObject。

但該應用程序崩潰。

然後我取消註釋了SpriteObject發佈行,但它仍然崩潰。

然後我在return語句中刪除了兩個autorelease,它的工作原理是 。

有人可以根據上面的代碼給一些建議? 在此先感謝。

回答

1

哦,男孩,有多個問題。首先記得發佈一切你稱之爲「alloc,new,retain,copy」的東西。請參閱Objective C release, autorelease, and data typesRelease, Dealloc, and the Self reference

這裏的基本問題是,您正在覆蓋以前創建的變量。例如:

NSMutableArray * array = [[NSMutableArray alloc] init];

array = ...;

[array release];

因此,您創建一個內存泄漏,並獲得雙發佈的麻煩。請參閱obj-c NSString and alloc/retain/release瞭解如何正確執行此操作。