I found circular reference under some condiction.
When add schedule in layer/scene , the schedule retain its target ,
this could cause the layer/scene not release after replaceScene
why and how to solve?
A fast, easy to use, free, and community supported 2D game engine
I found circular reference under some condiction.
When add schedule in layer/scene , the schedule retain its target ,
this could cause the layer/scene not release after replaceScene
why and how to solve?
When the scene is replaced, everything should be released (unless your code is retaining something). If not, it's a bug.
If you want to remove it manually, do this:
[instance removeChild:child withCleanup:YES];
@implementation GameScene
-(id) init
{
if (!(self=[super init] )) return nil;
[self addChild: [ColorLayer layerWithColor: 0x00ff00ff]];
[self addChild: [GameLayer node]];
[self schedule: @selector(step:)]; //this cause the problem!
return self;
}
now,i upload the testcode:
http://code.google.com/p/cocos2d-iphone/issues/detail?id=419
I am having the same problem. How do I make sure that the retain count is set to zero when I replace the scene? The scheduler is never released. I have tried creating a separate function with unschedule in it but it does not decrement the retain count.
Here is my code:
-(id) init
{
time_limit = 0;
NSLog(@"Scene retainCount : %d", [self retainCount]); //prints 1
//sets up timer to change to MenuScene after 2 seconds
[self schedule: @selector(step2:) interval:1.0f];
NSLog(@"Scene retainCount : %d", [self retainCount]); //prints 2
}
-(void) step2
{
if(time_limit == 2)
{
//[self removeAllChildrenWithCleanup:YES];
MenuScene * ms = [MenuScene node];
[[Director sharedDirector] replaceScene:[FadeTransition transitionWithDuration:1.0f scene:ms]];
NSLog(@"Scene retainCount : %d", [self retainCount]); //prints 3
//how do i get the retainCount down to 0 so my scene is released???
}
time_limit +=1;
}
I think it's already fixed. try with the lastest SVN revision
Thanks for the reply riq. I didn't know what SVN meant so I googled it. Does that mean that I should install cocos2d version 0.8 or are you referring to an updated version of cocos2d version 0.7.3? Please advise.
@deeniri: the lastest SVN version is v0.8_beta + patches.
to download it you can do:
svn checkout http://cocos2d-iphone.googlecode.com/svn/trunk/ cocos2d-iphone-read-onlyriq,
I test with the lastest svn r1092, the problem is still there.
http://code.google.com/p/cocos2d-iphone/issues/detail?id=419
This does not appear to be fixed... I could be very wrong but I was able to fix this by doing [self unschedule:@selector(UpdateTime:)]; in OnExit. This releases the target from the timer. This may be how it is meant to be but Im not sure.
Issue still not fixed.
unschedule: does not release self
test case:
@implementation CocosNode (SchedulerTest)
- (void) doSomething:(ccTime) dt {}
@end
Layer *layer = [Layer node];
NSLog(@"retain count after init is %d", [layer retainCount]); // 1
[scene addChild:layer z:0];
NSLog(@"retain count after addChild is %d", [layer retainCount]); // 2
[layer schedule:@selector(doSomething:)];
NSLog(@"retain count after schedule is %d", [layer retainCount]); // 3
[layer unschedule:@selector(doSomething:)];
NSLog(@"retain count after unschedule is %d", [layer retainCount]); // STILL 3!
scene here is my subclass of standard Scene
I never saw the retain count with very good eyes, a few months ago debugging box2d deallocs i found that some methods would do a retain and autorelease on the object.
Instead of checking for the retain count, I would advice to just subclass Layer and place a NSLog on dealloc.
The reason I'm using NSLog is that dealloc is never called because of wrong retain count. You can see that unschedule method does not release target
Hey the problem is still there after two years -_-
CCScheduler.m line #240
element->target = [target retain];
My layers doesn't call dealloc and several hours later i found this retain.
The problem is that unscheduleSelector never call release, so the retainCount is always 2 and the system doesn't fire dealloc.
Should i open a bug ticket or something like that?
You must log in to post.