0

我知道 CCTMXTiledMap 是一个自动释放对象。我也知道 CCTMXTiledMap 不应该重新初始化。但是,我怎样才能改变场景?

更改场景时,我在 TMXTiledMap 对象上遇到了解除分配错误。这里是场景1的属性和dealloc方法。

@interface MapObj : NSObject {
CCTMXTiledMap *theMap;
CCTMXLayer *bgLayer;
CCTMXLayer *terrainLayer;
NSMutableArray *virtualMap;
NSUInteger h, w;
}

 @property (nonatomic, retain) CCTMXTiledMap *theMap;
 @property (nonatomic, retain) CCTMXLayer *bgLayer;
 @property (nonatomic, retain) CCTMXLayer *terrainLayer;
 @property (nonatomic, assign) NSMutableArray *virtualMap;

-(void) dealloc {

[bgLayer release];    //self.bgLayer = [theMap layerNamed:@"bg"];
self.bgLayer = nil;
[terrainLayer release];  //self.terrainLayer = [theMap layerNamed:@"pl"];
self.terrainLayer = nil;

[theMap release];  //self.theMap = [CCTMXTiledMap tiledMapWithTMXFile:@"newmaptest.tmx"];
self.theMap = nil;

[virtualMap release];   //just an array, not related to TMX
self.virtualMap = nil;
[super dealloc]; }

我在场景更改时出错(场景 1 的释放不成功)

- (void) dealloc
{
CCLOGINFO( @"cocos2d: deallocing %@", self);

// attributes
[camera_ release];

[grid_ release];

// children
CCNode *child;
CCARRAY_FOREACH(children_, child)
    child.parent = nil; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<EXC_BAD_ACCESS

[children_ release];

[super dealloc];}

这是日志

2012-11-16 16:37:10.402 VerBeta[4876:307] cocos2d: deallocing <CCScene = 0024F5D0 | Tag = -1>
2012-11-16 16:37:10.405 VerBeta[4876:307] cocos2d: deallocing <CCArray = 00233F80> = ( <Playground = 0022C880 | Tag = -1>, )
2012-11-16 16:37:10.408 VerBeta[4876:307] cocos2d: deallocing <CCTexture2D = 002373B0 | Name = 2 | Dimensions = 128x32 | Coordinates = (0.60, 0.88)>
2012-11-16 16:37:10.412 VerBeta[4876:307] cocos2d: deallocing <CCLabelTTF = 0023A220 | FontSize = 20.0>
2012-11-16 16:37:10.416 VerBeta[4876:307] cocos2d: deallocing <CCTexture2D = 00230B90 | Name = 11 | Dimensions = 128x32 | Coordinates = (0.76, 0.88)>
2012-11-16 16:37:10.419 VerBeta[4876:307] cocos2d: deallocing <CCLabelTTF = 00245040 | FontSize = 20.0>
2012-11-16 16:37:10.422 VerBeta[4876:307] cocos2d: deallocing <CCTMXTilesetInfo: 0x249e70>
2012-11-16 16:37:10.425 VerBeta[4876:307] cocos2d: deallocing <CCSprite = 0024B0B0 | Rect = (0.00,0.00,32.00,32.00) | tag = -1 | atlasIndex = 149>
2012-11-16 16:37:10.428 VerBeta[4876:307] cocos2d: deallocing <CCTextureAtlas = 00249C00 | totalQuads =  150>
2012-11-16 16:37:10.433 VerBeta[4876:307] cocos2d: deallocing <CCArray = 00249FB0> = ( )
2012-11-16 16:37:10.436 VerBeta[4876:307] cocos2d: deallocing <CCTMXLayer = 0024ADC0 | Tag = 0>
2012-11-16 16:37:10.439 VerBeta[4876:307] cocos2d: deallocing <CCArray = 00249BA0> = ( )
2012-11-16 16:37:10.442 VerBeta[4876:307] cocos2d: deallocing <CCTMXTilesetInfo: 0x24a150>
2012-11-16 16:37:10.445 VerBeta[4876:307] cocos2d: deallocing <CCSprite = 0024BC00 | Rect = (32.00,0.00,32.00,32.00) | tag = -1 | atlasIndex = 149>
2012-11-16 16:37:10.448 VerBeta[4876:307] cocos2d: deallocing <CCTextureAtlas = 0024B560 | totalQuads =  150>
2012-11-16 16:37:10.452 VerBeta[4876:307] cocos2d: deallocing <CCArray = 002494C0> = ( )
2012-11-16 16:37:10.455 VerBeta[4876:307] cocos2d: deallocing <CCTMXLayer = 0024B400 | Tag = 1>
2012-11-16 16:37:10.457 VerBeta[4876:307] cocos2d: deallocing <CCArray = 00248DD0> = ( )
2012-11-16 16:37:10.461 VerBeta[4876:307] cocos2d: deallocing <CCTMXTiledMap = 002474A0 | Tag = -1>

我试过不发布theMap。比如注释掉这两行

//[theMap release];
//self.theMap = nil;

但是错误仍然发生,日志在这里

2012-11-16 16:34:21.000 VerBeta[4850:307] cocos2d: deallocing <CCScene = 00242580 | Tag = -1>
2012-11-16 16:34:21.003 VerBeta[4850:307] cocos2d: deallocing <CCArray = 00226F30> = ( <Playground = 0021F830 | Tag = -1>, )
2012-11-16 16:34:21.007 VerBeta[4850:307] cocos2d: deallocing <CCTexture2D = 00225190 | Name = 2 | Dimensions = 128x32 | Coordinates = (0.60, 0.88)>
2012-11-16 16:34:21.010 VerBeta[4850:307] cocos2d: deallocing <CCLabelTTF = 0022D1D0 | FontSize = 20.0>
2012-11-16 16:34:21.014 VerBeta[4850:307] cocos2d: deallocing <CCTexture2D = 0022D0A0 | Name = 3 | Dimensions = 128x32 | Coordinates = (0.76, 0.88)>
2012-11-16 16:34:21.017 VerBeta[4850:307] cocos2d: deallocing <CCLabelTTF = 00237FF0 | FontSize = 20.0>
2012-11-16 16:34:21.021 VerBeta[4850:307] cocos2d: deallocing <CCTMXTilesetInfo: 0x23ce20>
2012-11-16 16:34:21.024 VerBeta[4850:307] cocos2d: deallocing <CCSprite = 0023E060 | Rect = (0.00,0.00,32.00,32.00) | tag = -1 | atlasIndex = 149>
2012-11-16 16:34:21.027 VerBeta[4850:307] cocos2d: deallocing <CCTextureAtlas = 0023CBB0 | totalQuads =  150>
2012-11-16 16:34:21.030 VerBeta[4850:307] cocos2d: deallocing <CCArray = 0023CF60> = ( )
2012-11-16 16:34:21.034 VerBeta[4850:307] cocos2d: deallocing <CCTMXLayer = 0023DD70 | Tag = 0>
2012-11-16 16:34:21.038 VerBeta[4850:307] cocos2d: deallocing <CCArray = 0023CB50> = ( )
2012-11-16 16:34:21.041 VerBeta[4850:307] cocos2d: deallocing <CCTMXTilesetInfo: 0x23d100>
2012-11-16 16:34:21.044 VerBeta[4850:307] cocos2d: deallocing <CCSprite = 0023EBB0 | Rect = (32.00,0.00,32.00,32.00) | tag = -1 | atlasIndex = 149>
2012-11-16 16:34:21.047 VerBeta[4850:307] cocos2d: deallocing <CCTextureAtlas = 0023E510 | totalQuads =  150>
2012-11-16 16:34:21.050 VerBeta[4850:307] cocos2d: deallocing <CCArray = 0023C470> = ( )
2012-11-16 16:34:21.053 VerBeta[4850:307] cocos2d: deallocing <CCTMXLayer = 0023E3B0 | Tag = 1>
2012-11-16 16:34:21.056 VerBeta[4850:307] cocos2d: deallocing <CCArray = 0023BD80> = ( )
VerBeta(4850,0x3e29f898) malloc: *** error for object 0x23b660: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
4

1 回答 1

0

如果您没有将 ivar 分配给属性,则不应调用[propertyName release],而应使用[_propertyName release];

于 2012-11-16T13:21:59.413 回答