I have a fairly simple app going that开发者_StackOverflow I have a problem with. It works fine for a few seconds, but after a while, it crashes. Here's my code:
-(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;
}
And here's the session data from the debugger console:
[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>
Right when it crashes is when it gives that deallocing statement. Any idea what's going on here?
Additional details: I've tried replacing the 'autorelease' in the trail initialization with 'retain' and removing it completely, and neither seems to make a difference.
The autorelease
in this line causes the problem:
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:
returns an autoreleased object. Since this code autoreleases it again, it gets too many release calls when the autorelease pool is drained.
精彩评论