Rendel
Rendel

Reputation: 61

Release all object after replacing the scene

I have two class A and B. I create objects of the class B in the A:

B *objectB = [B classInitWithParamiters:paramiters];
[self addChile:objecTB z:1 tag:varForTag];
varForTag++;

I call this code many times.

This is B.h file:

@interface Chicken : CCSprite <CCTargetedTouchDelegate> {
    CCsprite *spriteB;
}
+ (id) classInitWithParamiters :(int) paramiters;

This is B.m file:

+ (id) classInitWithParamiters :(int) paramiters
{
    return [[[self alloc] init] autorelease];
}
- (id) init
{
    if( (self = [super init]) ) {
        [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:NO];
    spriteB = [[CCSprite alloc] initWithFile:@"image.png"];
    spriteB.position = ccp(160, 240);
    [self addChild:spriteB];
    }
    return self;
}
- (void) update :(ccTime)dt
{
    NSLog(@"This is a Class B");
}
- (void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint location = [[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]];

    if(CGrectContainsPoint([spriteB boundingbox], location))
        NSLog(@"touch moved in the class B");
}

My problem is: When I replace A scene with scene of the class C, the method update of the class B stops the log, but if I touch the middle of the screen and move the finger it logs "touch moved in the class B".
What I'm doing wrong? These objects of the class B should not released automatically after replacing the scene. Class B is subclass of the CCSprite and A - CCLayer;

Upvotes: 0

Views: 103

Answers (2)

CodeSmile
CodeSmile

Reputation: 64477

Class B is obviously still running. Which means it leaked and never got shut down by cocos2d. Hence it's still receiving touches and may even be running scheduled updates and actions.

My guess is you introduced a retain cycle. Typical cause is a node with a retaining reference to a different node that is not one of its children or grandchildren. For example, retaining the scene node in a child node can cause a retain cycle, if the scene node is not released/nil'ed in the cleanup method (dealloc will not be called if the scene is still retained, so cleanup is the only place to clean up such potential retain cycle references).

Upvotes: 2

OnkarK
OnkarK

Reputation: 3793

Your problem is in Class B:

+ (id) classInitWithParamiters :(int) paramiters
{
    [[[self alloc] init] autorelease];

}

You must have to return object.

+ (id) classInitWithParamiters :(int) paramiters
{
    return [[[self alloc] init] autorelease];

}

Upvotes: 0

Related Questions