Can this part of a view controller subclass be checked for memory leaks. I am not that good at finding leaks. I need this class to be able to be loaded many time over without it crashing due to memory leaks. The endgame gets called every time it needs to go back to the main menu. I want it to be able to go back to the main menu without leaving any memory footprint.

UIImageView* questionImage;
UIImageView* questionImage2;

UIButton* questionButton1;
UIButton* questionButton2;
UIButton* questionButton3;
UIButton* questionButton4;
UIButton* questionButton5;
UIButton* questionButton6;

UIButton* achievementbutton;
UIButton* endbutton;

UILabel* questionText;
UILabel* ButtonLabel;

- (void)viewDidLoad 
    [super viewDidLoad];
    [self StartTimer];

    TotalSeconds = 0;
    GameCenterTotalSeconds = 0;
    timeSec = 0;
    timeMin = 0;

    Background = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 480, 320)] ;
    Background.image = [UIImage imageWithContentsOfFile:[ [ NSBundle mainBundle] pathForResource:@"Background" ofType:@"png"]];
    [self.view addSubview:Background];

    timeLabel.textColor = [UIColor redColor];
    [self.view addSubview:timeLabel];

    NumberLabel = [[UIImageView alloc] initWithFrame:CGRectMake(0, -4, 60, 70)] ;
    NumberLabel.image = [UIImage imageWithContentsOfFile:[[ NSBundle mainBundle] pathForResource:@"Number" ofType:@"png"]];
    [self.view addSubview:NumberLabel];

    QuestionNumber = [[UILabel alloc] initWithFrame:CGRectMake(23, 17, 20, 20)] ;
    QuestionNumber.text = @"1";
    QuestionNumber.textColor = [UIColor redColor];
    QuestionNumber.backgroundColor = [UIColor clearColor];
    [QuestionNumber setFont:[UIFont fontWithName:@"Marker Felt" size:30]];
    [self.view addSubview:QuestionNumber];

    numberLives = 1;

    appDelegate = (OppositeMoronTestAppDelegate *)[[UIApplication sharedApplication]delegate];

    musicButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain] ;
    musicButton.frame = CGRectMake(5, 283, 35, 35);
    musicButton.backgroundColor = [UIColor clearColor];

    if (appDelegate.shouldPlayMusic == YES) {

        UIImage *Image = [UIImage imageWithContentsOfFile:[[ NSBundle mainBundle] pathForResource:@"MusicOn" ofType:@"png"]];
        [musicButton setBackgroundImage:Image forState:UIControlStateNormal];
        [musicButton addTarget:self action:@selector(TurnMusicOff) forControlEvents:UIControlEventTouchUpInside];
    } else {
        UIImage *Image = [UIImage imageWithContentsOfFile:[[ NSBundle mainBundle] pathForResource:@"MusicOff" ofType:@"png"]];
        [musicButton setBackgroundImage:Image forState:UIControlStateNormal];
        [musicButton addTarget:self action:@selector(TurnMusicOn) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:musicButton];
    [self showQuestion1];

- (void) showQuestion1 
    questionImage = [[UIImageView alloc] initWithFrame:CGRectMake(15, 50, 430, 270)] ;
    questionImage.image = [UIImage imageWithContentsOfFile:[[ NSBundle mainBundle] pathForResource:@"Q1new" ofType:@"png"]];
    [self.view addSubview:questionImage];

    questionButton5 = [UIButton buttonWithType:UIButtonTypeCustom];
    [questionButton5 addTarget:self action:@selector(showQuestion2Part1)forControlEvents:UIControlEventTouchDown];
    questionButton5.frame = CGRectMake(109, 75, 90, 65);
    [self.view addSubview:questionButton5];

    questionButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [questionButton1 addTarget:self action:@selector(wronganswer)forControlEvents:UIControlEventTouchDown];
    questionButton1.frame = CGRectMake(230, 50, 120, 90);
    [self.view addSubview:questionButton1];

    questionButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [questionButton2 addTarget:self action:@selector(wronganswer)forControlEvents:UIControlEventTouchDown];
    questionButton2.frame = CGRectMake(300, 144, 100, 90);
    [self.view addSubview:questionButton2];

    questionButton3 = [UIButton buttonWithType:UIButtonTypeCustom];
    [questionButton3 addTarget:self action:@selector(wronganswer)forControlEvents:UIControlEventTouchDown];
    questionButton3.frame = CGRectMake(203, 187, 95, 90);
    [self.view addSubview:questionButton3];

    questionButton4 = [UIButton buttonWithType:UIButtonTypeCustom];
    [questionButton4 addTarget:self action:@selector(wronganswer)forControlEvents:UIControlEventTouchDown];
    questionButton4.frame = CGRectMake(67, 140, 120, 95);
    [self.view addSubview:questionButton4];

    questionText = [[UILabel alloc] initWithFrame:CGRectMake(80, 20, 300, 30)] ;
    questionText.text = @"Press the Addition Sign";
    questionText.textColor = [UIColor blackColor];
    questionText.backgroundColor = [UIColor clearColor];
    [questionText setFont:[UIFont fontWithName:@"Marker Felt" size:30]];
    [self.view addSubview:questionText];
- (void) endgame 
    [WrongBackground release];
    [gameOver release];
    [wrongLivesLeft release];
    [questionImage release];
    [questionText release];
    [Background release];
    [NumberLabel release];
    [musicButton release];
    [questionImage release];
    [questionText release];

    [self.view removeFromSuperview];
  • 2
    \$\begingroup\$ You have to indent your code with the {} - button in the edit box. And look at it in the preview, which is below your edit section. Didn't you read the FAQ and HOWTOS? The posts in meta? \$\endgroup\$ Commented Jul 14, 2011 at 2:01
  • 1
    \$\begingroup\$ This is what Instruments in the dev package is for. \$\endgroup\$ Commented Jul 15, 2011 at 12:49
  • \$\begingroup\$ Also; try "Build and Analyze" as the Clang Static Humiliator is quite good at identifying incorrect memory management. \$\endgroup\$
    – bbum
    Commented Nov 29, 2011 at 22:01

1 Answer 1


There are several views that you're allocating and installing as subviews in -viewDidLoad. Among these are NumberLabel, Background, and QuestionNumber. (BTW, it'd be a little easier to follow your code if you stuck to Objective-C naming conventions and start variables with a lower-case letter.) These appear to be instance variables, but you didn't provide the class declaration so it's hard to be certain. Anyway, if you don't need to refer to these views again after their added as to the main view, it would be better to make them local variables instead of ivars and release them right away. (The view will retain them once they've been added.) When the view is unloaded, all those subviews will be released.

QuestionNumber isn't released in the code you've provided -- that may be a leak.

It's not clear where timelabel comes from or if it actually points to a view.

You're releasing WrongBackground, but that's not created in -viewDidLoad, so it's hard to know if that's correct or not.

There are several variables like questionButton1 and friends that look like file-scope global variables rather than instance variables, but there's no indication of why. Wouldn't it make sense to make them ivars?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.