1
votes

My problem is, that some layers nerver gets deallocated. My previews conviction was, that [CCDirector sharedDirector] replaceScene:[Menu node]]; or similar does everything for me. There stays active layers in the background, if I load a new layer. That leads to weird bugs. Anyway, the code at the bottom has a retain count of 2 while the onExit call. Dealloc is never called. To remove the rootVC solves the problem. Unfortunately I need it.

#import "Help.h"
#import "Constants.h"
#import "MainMenu.h"


@implementation Help

-(void)showText:(ccTime)dt
{
    [self unschedule:_cmd];
    txt.hidden = NO;
}

-(void)onExit
{
    CCLOG(@"retain count %i", [self retainCount]);
    [super onExit];
}

-(id) init
{
    if ( (self = [super init]) ) 
    {
        NSString* bgPath;
        NSString* button2Path;
        NSString* button2PressPath;
        CGRect textFrame;
        int fontsize;
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            bgPath = @"background-ipad.png";
            button2Path = @"button2-ipad.png";
            button2PressPath = @"button2_press-ipad.png";
            textFrame = CGRectMake(60, 140, 920, 500);
            fontsize = 24;
        } else {
            bgPath = @"background.png";
            button2Path = @"button2.png";
            button2PressPath = @"button2_press.png";
            textFrame = CGRectMake(10, 45, 460, 217);
            fontsize = [kMenuFontSize intValue];
        }

        NSString* helpText = NSLocalizedString(@"Bla bla bla yada yada yada", @"Helptext");



        rootVC = [UIApplication sharedApplication].keyWindow.rootViewController; // +1 retain to the layer
        txt = [[[UITextView alloc] initWithFrame:textFrame] autorelease];
        txt.text = helpText;
        [txt setEditable:NO];
        [txt setFont:[UIFont fontWithName:@"Sui Generis" size:fontsize]];
        [txt setTextColor:[UIColor whiteColor]];
        [txt setBackgroundColor:[UIColor clearColor]];
        [rootVC.view addSubview:txt];
        txt.hidden = YES;
        [self schedule:@selector(showText:) interval:1];

        self.isTouchEnabled = NO;

        CGSize ss = [[CCDirector sharedDirector] winSize];

        CCSprite* background = [CCSprite spriteWithFile:bgPath];
        background.position = ccp(ss.width*0.5, ss.height*0.5);
        [self addChild:background z:0];

        CCLabelTTF* backLabel = [CCLabelTTF labelWithString:NSLocalizedString(@"Back", @"back button at help") fontName:kMenuFont fontSize:fontsize];
        backLabel.position = ccp(ss.width * 0.1, ss.height * 0.1);
        [backLabel setColor:ccBLACK];
        [self addChild:backLabel z:20];
        CCMenuItemImage* backButton = [CCMenuItemImage itemFromNormalImage:button2Path selectedImage:button2PressPath block:^(id sender) {

            [txt removeFromSuperview];
            id trans = [CCTransitionFade transitionWithDuration:1 scene:[MainMenu node]];
            [[CCDirector sharedDirector] replaceScene:trans];
        } ];
        backButton.position = ccp(ss.width * 0.1, ss.height * 0.1);
        CCMenu* menu = [CCMenu menuWithItems:backButton, nil];
        [menu setPosition:ccp(0,0)];
        [self addChild:menu z:10];

    }
    return self;
}

-(void)dealloc
{
    CCLOG(@"Help dealloc");
    [super dealloc];
}

@end
1

1 Answers

4
votes

Do not call retainCount

It is useless.

You need to make sure your retains and releases are balanced. Sometimes, this means making sure that you have correctly torn down any dependent state that may be retaining your object.

For example, this:

    [self schedule:@selector(showText:) interval:1];

If it is using an NSTimer with self as the target, it'll retain self. Since your onExit is being called, do you need to effectively "unschedule" self? (I haven't read the cocos2d source to know definitively).


How do I release rootVC = [UIApplication sharedApplication].keyWindow.rootViewController; // +1 retain to the layer. [self release] doesn't work.

This question doesn't make any sense; calling methods that return objects should not increase the retain count and, obviously, releasing self isn't going to work.

It isn't clear what you are asking.