首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >记忆问题

记忆问题
EN

Code Review用户
提问于 2011-07-14 00:51:04
回答 1查看 603关注 0票数 2

是否可以检查视图控制器子类的这一部分是否存在内存泄漏。我不太擅长发现泄密。我需要这个类能够多次加载,而不会因为内存泄漏而崩溃。每次需要返回主菜单时,都会调用结束游戏。我希望它能够回到主菜单,而不留下任何内存占用。

代码语言:javascript
复制
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];
}
EN

回答 1

Code Review用户

发布于 2011-08-11 07:06:37

-viewDidLoad中有几个视图是作为子视图分配和安装的。其中包括NumberLabelBackgroundQuestionNumber。(顺便说一句,如果您坚持使用Objective命名约定并使用小写字母开始变量,那么您的代码就会容易一些。)这些似乎是实例变量,但是您没有提供类声明,所以很难确定。无论如何,如果您不需要在添加到主视图之后再次引用这些视图,最好是让它们成为局部变量,而不是ivars,并立即释放它们。(一旦添加了它们,视图将保留它们。)卸载视图时,将释放所有这些子视图。

QuestionNumber不是在您提供的代码中发布的--这可能是一个漏洞。

目前还不清楚timelabel是从哪里来的,也不清楚它是否真正指向视图。

您正在发布WrongBackground,但这不是在-viewDidLoad中创建的,因此很难知道这是否正确。

questionButton1和朋友这样的变量看起来像文件范围的全局变量,而不是实例变量,但是没有说明原因。把他们变成象牙不是有道理的吗?

票数 3
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/3430

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档