我有一个相当简单的应用程序,我有一个问题。它工作正常几秒钟,但过了一会儿,它崩溃了。这是我的代码:
-(id) init
{
if( (self=[super init] )) {
self.isTouchEnabled = YES;
trail = [[CCMotionStreak streakWithFade:3.0f minSeg:1 image:@"streak.png" width:6 length:50 color:ccc4(120, 250, 150, 200)] autorelease];
[self addChild:trail z:1 tag:2];
}
return self;
}
-(BOOL) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self ccTouchesMoved:touches withEvent:event];
return YES;
}
-(BOOL)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView: [touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
[trail setPosition:touchLocation];
return YES;
}
-(BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
return YES;
}
这是来自调试器控制台的会话数据:
[Session started at 2011-06-21 14:37:41 +0800.]
2011-06-21 14:37:49.933 Particle[3587:207] cocos2d: cocos2d v0.9.0 beta2
2011-06-21 14:37:49.936 Particle[3587:207] cocos2d: Using Director Type:CCDisplayLinkDirector
2011-06-21 14:37:50.313 Particle[3587:207] cocos2d: Frame interval: 1
2011-06-21 14:38:01.880 Particle[3587:207] cocos2d: deallocing <CCRibbonSegment = 0603D000 | end = 50, begin = 50>
崩溃的时候就是它给出了deallocing语句。知道这里发生了什么吗?
其他细节:我尝试用'保留'替换路径初始化中的'autorelease'并将其完全删除,但似乎没有任何区别。
答案 0 :(得分:1)
此行中的autorelease
会导致问题:
trail = [[CCMotionStreak streakWithFade:3.0f minSeg:1 image:@"streak.png" width:6 length:50 color:ccc4(120, 250, 150, 200)] autorelease];
streakWithFade:minSeg:image:width:length:color:
返回一个自动释放的对象。由于此代码再次自动释放,因此在自动释放池耗尽时会释放太多的释放调用。