2011-11-16 6 views
0

私はクラスTileMapを持っています。それはメモリにレベルをロードし、幅と高さを読み取ります。私は自動解放でこのクラスを呼び出すときにでも - >プログラムは、私が書くとき、別のクラスでの.mautoreleaseのクラッシュ(cocos2d)

#import "TileMap.h" 

@implementation TileMap 

@synthesize tileMap = _tileMap; 
@synthesize width = _width; 
@synthesize height = _height; 

- (void) dealloc 
{ 
    //[_tileMap release]; 
    self.tileMap = nil; 
    [super dealloc]; 
} 

-(void) loadMapWithLVL: (NSString *)lvl 
{ 
    [self.tileMap release]; 
    NSString *lvl_new = [NSString stringWithFormat:@"%@.tmx",lvl]; 

    CCLOG(@"Reload TileMap to %@", lvl_new); 
    self.tileMap = [[CCTMXTiledMap tiledMapWithTMXFile:lvl_new] retain]; 
    self.width = _tileMap.mapSize.width; 
    self.height = _tileMap.mapSize.height;  
} 

- (id)init 
{ 
    self = [super init]; 
    if (self) { 
     CCLOG(@"Init TileMap"); 
     self.tileMap = [CCTMXTiledMap tiledMapWithTMXFile:@"1lvl.tmx"]; 
     self.width = _tileMap.mapSize.width; 
     self.height = _tileMap.mapSize.height; 
     CCLOG(@"size map in init %0.2f %0.2f", _width, _height); 

    }   
    return self; 
}  
@end 

での.h

#import "cocos2d.h" 

@interface TileMap : NSObject 
{ 
    CCTMXTiledMap *_tileMap; 
    float _width, _height; 
} 

@property (nonatomic, retain) CCTMXTiledMap *tileMap; 
@property (readwrite) float width; 
@property (readwrite) float height; 

-(void) loadMapWithLVL : (NSString *)lvl; 
@end 

をクラッシュ:

TileMap *testmap = [[[TileMap alloc] init] autorelease]; 
    float testg = testmap.width; 
    CCLOG(@"size map %0.2f", testg); 

私はクラッシュしている。しかし、私が書くとき:

TileMap *testmap = [[TileMap alloc] init]; 
float testg = testmap.width; 
CCLOG(@"size map %0.2f", testg); 
[testmap release] 

私はクラッシュしていません。なぜこうなった?

答えて

1

コードに間違いがいくつかあります。最初は、プロパティを '保持' - @property (nonatomic, retain) CCTMXTiledMap *tileMap;と宣言し、同様に '合成'します。つまり、release/retainキーワードなしでこのプロパティで操作できるようになりました。 - あなたのきれいな性質dealloc方法でself.を呼び出さないでください

-(void) loadMapWithLVL: (NSString *)lvl{ 
    NSString *lvl_new = [NSString stringWithFormat:@"%@.tmx",lvl]; 

    CCLOG(@"Reload TileMap to %@", lvl_new); 
    self.tileMap = [CCTMXTiledMap tiledMapWithTMXFile:lvl_new]; 
    self.width = _tileMap.mapSize.width; 
    self.height = _tileMap.mapSize.height;  
} 

もう一つ:だから、このようなメソッドloadMapWithLVLを書き換えます。 [_tileMap release];, _titleMap = nil;は、メモリをクリーニングするための正しい方法です。

関連する問題