2011-08-18 51 views
0

我创建为iPhone/iPad的(通用的应用程序)。它测验的应用程序工作正常,在模拟器,但在真机上也有一些问题,我面对..多时间的分配和释放

  • 我以编程方式创建UILabels和UIButton,并且我将它们用于多次显示问题和答案。

问题是当我达到约问题20至22中的应用程序崩溃。我不知道为什么。没有发现内存泄漏。我释放了-dealloc函数中的所有标签和按钮。
可能会发生,因为22个问题需要一些内存,直到dealloc函数调用才会释放。

我要的是,有没有什么办法来释放每一个问题后,标签和按钮,然后它会再次分配?或者我可以使用更少的内存分配标签和按钮的任何方式?

我使用这个代码:

//lbl ref to btn1 
ans1 = [[[UILabel alloc] initWithFrame:CGRectMake(36, 292, 697, 100)] autorelease]; 
ans1.numberOfLines = 0; 
ans1.font = [UIFont systemFontOfSize:26]; 
ans1.text = [theQuiz objectAtIndex:row+1]; 
ans1.lineBreakMode = UILineBreakModeWordWrap; 
[ans1 sizeToFit]; 
//[self.view addSubview:ans1]; 
//lbl ref to btn2 
ans2 = [[[UILabel alloc] initWithFrame:CGRectMake(36, 312 + ans1.frame.size.height , 697, 100)] autorelease]; 
ans2.numberOfLines = 0; 
ans2.font = [UIFont systemFontOfSize:26]; 
ans2.text = [theQuiz objectAtIndex:row+2]; 
ans2.lineBreakMode = UILineBreakModeWordWrap; 
[ans2 sizeToFit]; 
// [self.view addSubview:ans2]; 
//lbl ref to btn3 
ans3 = [[[UILabel alloc] initWithFrame:CGRectMake(36 ,332 + ans1.frame.size.height + ans2.frame.size.height , 697 , 100)] autorelease]; 
ans3.numberOfLines = 0; 
ans3.font = [UIFont systemFontOfSize:26]; 
ans3.text = [theQuiz objectAtIndex:row+3] ; 
ans3.lineBreakMode = UILineBreakModeWordWrap; 
[ans3 sizeToFit]; 
// [self.view addSubview:ans3]; 
//lbl ref to btn4 
ans4 = [[[UILabel alloc] initWithFrame:CGRectMake(36, 352 + ans1.frame.size.height + ans2.frame.size.height + ans3.frame.size.height, 697, 100)] autorelease]; 
ans4.numberOfLines = 0; 
ans4.font = [UIFont systemFontOfSize:26]; 
ans4.text = [theQuiz objectAtIndex:row+4]; 
ans4.lineBreakMode = UILineBreakModeWordWrap; 
[ans4 sizeToFit]; 
// [self.view addSubview:ans4]; 

rightAnswer = [[theQuiz objectAtIndex:row+5] intValue]; 

Question = [[[UILabel alloc] initWithFrame:CGRectMake(22, 130, 725, 160)] autorelease]; 
Question.numberOfLines = 0; 
//Question.font = [UIFont systemFontOfSize:27]; 
Question.text = [NSString stringWithFormat:@"%@" ,selected1]; 
Question.lineBreakMode = UILineBreakModeWordWrap; 
[Question setFont:[UIFont fontWithName:@"Futura" size:26]]; 
Question.backgroundColor = [UIColor clearColor]; 
[Question sizeToFit]; 
[self.view addSubview:Question]; 

//For 1st Option. 
button = [UIButton buttonWithType:UIButtonTypeCustom]; 
[button addTarget:self 
      action:@selector(buttonOne) 
forControlEvents:UIControlEventTouchDown]; 
button.titleLabel.lineBreakMode = UILineBreakModeWordWrap; 
button.titleLabel.font = [UIFont systemFontOfSize:24]; 
[button setTitle:[theQuiz objectAtIndex:row+1] forState:UIControlStateNormal]; 
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
button.frame = CGRectMake(34, 200 + Question.frame.size.height , 700, ans1.frame.size.height + 20) ; 
[self.view addSubview:button]; 
button.layer.borderWidth = 3; 
button.layer.borderColor = [[UIColor purpleColor ] CGColor]; 
button.layer.cornerRadius = 10.0; 
[button setBackgroundImage:[UIImage imageNamed:@"buttun.png"] forState:UIControlStateNormal]; 
button.titleEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5); 

//For 2nd Option. 
button2 = [UIButton buttonWithType:UIButtonTypeCustom]; 
[button2 addTarget:self 
      action:@selector(buttonTwo) 
    forControlEvents:UIControlEventTouchDown]; 
[button2 setTitle:[theQuiz objectAtIndex:row+2] forState:UIControlStateNormal]; 
[button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
button2.titleLabel.lineBreakMode = UILineBreakModeWordWrap; 
button2.titleLabel.font = [UIFont systemFontOfSize:24]; 
button2.frame = CGRectMake(34, 230 + Question.frame.size.height + button.frame.size.height , 700, ans2.frame.size.height + 20); 
button2.layer.borderWidth = 3; 
button2.layer.borderColor = [[UIColor darkGrayColor ] CGColor]; 
button2.layer.cornerRadius = 10.0; 
[self.view addSubview:button2]; 
[button2 setBackgroundImage:[UIImage imageNamed:@"buttun2.png"] forState:UIControlStateNormal]; 
button2.titleEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5); 

//For 3rd Option. 
button3 = [UIButton buttonWithType:UIButtonTypeCustom]; 
[button3 addTarget:self 
      action:@selector(buttonThree) 
    forControlEvents:UIControlEventTouchDown]; 
[button3 setTitle:[theQuiz objectAtIndex:row+3] forState:UIControlStateNormal]; 
[button3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
button3.titleLabel.lineBreakMode = UILineBreakModeWordWrap; 
button3.titleLabel.font = [UIFont systemFontOfSize:24]; 
button3.frame = CGRectMake(34, 260 + Question.frame.size.height + button.frame.size.height + button2.frame.size.height , 700, ans3.frame.size.height + 20); 
button3.layer.borderWidth = 3; 
button3.layer.borderColor = [[UIColor purpleColor ] CGColor]; 
button3.layer.cornerRadius = 10.0; 

[self.view addSubview:button3]; 
[button3 setBackgroundImage:[UIImage imageNamed:@"buttun.png"] forState:UIControlStateNormal]; 
button3.titleEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5); 

//For 4th Option. 
button4 = [UIButton buttonWithType:UIButtonTypeCustom]; 
[button4 addTarget:self 
      action:@selector(buttonFour) 
    forControlEvents:UIControlEventTouchDown]; 
[button4 setTitle:[theQuiz objectAtIndex:row+4] forState:UIControlStateNormal]; 
[button4 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
button4.titleLabel.lineBreakMode = UILineBreakModeWordWrap; 
button4.titleLabel.font = [UIFont systemFontOfSize:24]; 
button4.frame = CGRectMake(34, 290 + Question.frame.size.height + button.frame.size.height + button2.frame.size.height + button3.frame.size.height , 700, ans4.frame.size.height + 20); 
button4.layer.borderWidth = 3; 
button4.layer.borderColor = [[UIColor darkGrayColor ] CGColor]; 
button4.layer.cornerRadius = 10.0; 
[self.view addSubview:button4]; 
[button4 setBackgroundImage:[UIImage imageNamed:@"buttun2.png"] forState:UIControlStateNormal]; 
button4.titleEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5); 

NSString *strright = [theQuiz objectAtIndex:row+7]; 
//NSString *strtext = [[NSString alloc] initWithFormat:@"%@" , strright]; 

rightans = [[[UILabel alloc] initWithFrame:CGRectMake(50, 330, 650, 150)]autorelease]; 
rightans.numberOfLines = 0; 
rightans.text = [NSString stringWithFormat:@"%@" , strright]; 
rightans.backgroundColor = [UIColor clearColor]; 
rightans.font = [UIFont systemFontOfSize:26]; 
[self.view addSubview:rightans]; 
[rightans sizeToFit]; 
[rightans setHidden:YES]; 

//Description. 
NSString *des = [theQuiz objectAtIndex:row+6]; 
// NSString *destext = [[NSString alloc] initWithFormat:@"\n> Explanation :\n%@\n\n" , des]; 

Description = [[[UILabel alloc] initWithFrame:CGRectMake(40, 400 + rightans.frame.size.height , 690, 150)] autorelease]; 
Description.numberOfLines = 0; 
// Description.font = [UIFont systemFontOfSize:13.5]; 
Description.text = [NSString stringWithFormat:@"\n> Explanation :\n%@\n\n" , des]; 
[Description setFont:[UIFont fontWithName:@"Futura" size:26]]; 
Description.lineBreakMode = UILineBreakModeWordWrap; 
Description.backgroundColor = [UIColor clearColor]; 
[Description sizeToFit]; 
[self.view addSubview:Description]; 
[Description setHidden:YES]; 


ContainerView = [[UIView alloc] initWithFrame:CGRectMake(30, 400 + rightans.frame.size.height , 700 ,Description.frame.size.height)]; 
ContainerView.backgroundColor = [UIColor clearColor]; 
ContainerView.layer.cornerRadius = 10.0; 
ContainerView.layer.borderColor = [[UIColor grayColor] CGColor]; 
ContainerView.layer.borderWidth = 3; 
// [ContainerView addSubview:Description]; 
[self.view addSubview:ContainerView]; 
[ContainerView setHidden:YES]; 

任何帮助?
谢谢。

回答

2

你不应该释放autorelease对象。

1

首先,你应该尽快将其添加到使用[self.view addSubview: myObj]视图释放的对象。当你像这样添加一个对象时,它的保留计数会通过父视图&增加,你应该释放它(减少其保留计数)以允许其垃圾收集。你的代码应该是 -

[self.view addSubview: myObj]; 
[myObj release]; 

其次,你有没有崩溃日志?

HTH,

阿克沙伊

+0

我再次使用这个标签来查看第二个问题..如果我在这里释放它,那么应用程序将停止在这一点..因为我的标签发布,所以没有分配给我的下一个问题。 – iUser

+0

那么,你在下一个问题之后发布它吗?此外,只有崩溃日志(无论出现在控制台上)可以帮助我们处理崩溃。 – Akshay

2
  1. 一旦你添加标签作为子视图的视图 - 视图将保留就可以释放标签的标签,在你的情况你是不是释放ContainerView
  2. 你不应该释放自动释放的对象 - 这是好的
-1

1)简单的规则,你释放任何你分开或保留,复制...所有其他变量哟你将会被自动释放。在您的代码中,您正在分配所有标签,但不是按钮..您必须在将其添加到子视图后释放所有标签。按钮你只需要添加到子视图,而不是后记因为你不分配它,因为你问similiar问题2天,我只是工作在你的代码

//lbl ref to btn1 
ans1 = [[[UILabel alloc] initWithFrame:CGRectMake(36, 292, 697, 100)] autorelease]; 
ans1.numberOfLines = 0; 
ans1.font = [UIFont systemFontOfSize:26]; 
ans1.text = [theQuiz objectAtIndex:row+1]; 
ans1.lineBreakMode = UILineBreakModeWordWrap; 
[ans1 sizeToFit]; 
[self.view addSubview:ans1]; 


//lbl ref to btn2 
ans2 = [[[UILabel alloc] initWithFrame:CGRectMake(36, 312 + ans1.frame.size.height , 697, 100)] autorelease]; 
ans2.numberOfLines = 0; 
ans2.font = [UIFont systemFontOfSize:26]; 
ans2.text = [theQuiz objectAtIndex:row+2]; 
ans2.lineBreakMode = UILineBreakModeWordWrap; 
[ans2 sizeToFit]; 
[self.view addSubview:ans2]; 


//lbl ref to btn3 
ans3 = [[[UILabel alloc] initWithFrame:CGRectMake(36 ,332 + ans1.frame.size.height + ans2.frame.size.height , 697 , 100)] autorelease]; 
ans3.numberOfLines = 0; 
ans3.font = [UIFont systemFontOfSize:26]; 
ans3.text = [theQuiz objectAtIndex:row+3] ; 
ans3.lineBreakMode = UILineBreakModeWordWrap; 
[ans3 sizeToFit]; 
[self.view addSubview:ans3]; 


//lbl ref to btn4 
ans4 = [[[UILabel alloc] initWithFrame:CGRectMake(36, 352 + ans1.frame.size.height + ans2.frame.size.height + ans3.frame.size.height, 697, 100)] autorelease]; 
ans4.numberOfLines = 0; 
ans4.font = [UIFont systemFontOfSize:26]; 
ans4.text = [theQuiz objectAtIndex:row+4]; 
ans4.lineBreakMode = UILineBreakModeWordWrap; 
[ans4 sizeToFit]; 
[self.view addSubview:ans4]; 

rightAnswer = [[theQuiz objectAtIndex:row+5] intValue]; 

Question = [[[UILabel alloc] initWithFrame:CGRectMake(22, 130, 725, 160)] autorelease]; 
Question.numberOfLines = 0; 
//Question.font = [UIFont systemFontOfSize:27]; 
Question.text = [NSString stringWithFormat:@"%@" ,selected1]; 
Question.lineBreakMode = UILineBreakModeWordWrap; 
[Question setFont:[UIFont fontWithName:@"Futura" size:26]]; 
Question.backgroundColor = [UIColor clearColor]; 
[Question sizeToFit]; 
[self.view addSubview:Question]; 

//For 1st Option. 
button = [UIButton buttonWithType:UIButtonTypeCustom]; 
[button addTarget:self action:@selector(buttonOne) forControlEvents:UIControlEventTouchDown]; 
button.titleLabel.lineBreakMode = UILineBreakModeWordWrap; 
button.titleLabel.font = [UIFont systemFontOfSize:24]; 
[button setTitle:[theQuiz objectAtIndex:row+1] forState:UIControlStateNormal]; 
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
button.frame = CGRectMake(34, 200 + Question.frame.size.height , 700, ans1.frame.size.height + 20) ; 
button.layer.borderWidth = 3; 
button.layer.borderColor = [[UIColor purpleColor ] CGColor]; 
button.layer.cornerRadius = 10.0; 
[button setBackgroundImage:[UIImage imageNamed:@"buttun.png"] forState:UIControlStateNormal]; 
button.titleEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5); 
[self.view addSubview:button]; 



//For 2nd Option. 
button2 = [UIButton buttonWithType:UIButtonTypeCustom]; 
[button2 addTarget:self action:@selector(buttonTwo) forControlEvents:UIControlEventTouchDown]; 
[button2 setTitle:[theQuiz objectAtIndex:row+2] forState:UIControlStateNormal]; 
[button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
button2.titleLabel.lineBreakMode = UILineBreakModeWordWrap; 
button2.titleLabel.font = [UIFont systemFontOfSize:24]; 
button2.frame = CGRectMake(34, 230 + Question.frame.size.height + button.frame.size.height , 700, ans2.frame.size.height + 20); 
button2.layer.borderWidth = 3; 
button2.layer.borderColor = [[UIColor darkGrayColor ] CGColor]; 
button2.layer.cornerRadius = 10.0; 
[button2 setBackgroundImage:[UIImage imageNamed:@"buttun2.png"] forState:UIControlStateNormal]; 
button2.titleEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5); 
[self.view addSubview:button2]; 


//For 3rd Option. 
button3 = [UIButton buttonWithType:UIButtonTypeCustom]; 
[button3 addTarget:self action:@selector(buttonThree) forControlEvents:UIControlEventTouchDown]; 
[button3 setTitle:[theQuiz objectAtIndex:row+3] forState:UIControlStateNormal]; 
[button3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
button3.titleLabel.lineBreakMode = UILineBreakModeWordWrap; 
button3.titleLabel.font = [UIFont systemFontOfSize:24]; 
button3.frame = CGRectMake(34, 260 + Question.frame.size.height + button.frame.size.height + button2.frame.size.height , 700, ans3.frame.size.height + 20); 
button3.layer.borderWidth = 3; 
button3.layer.borderColor = [[UIColor purpleColor ] CGColor]; 
button3.layer.cornerRadius = 10.0; 
[button3 setBackgroundImage:[UIImage imageNamed:@"buttun.png"] forState:UIControlStateNormal]; 
button3.titleEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5); 
[self.view addSubview:button3]; 



//For 4th Option. 
button4 = [UIButton buttonWithType:UIButtonTypeCustom]; 
[button4 addTarget:self action:@selector(buttonFour) forControlEvents:UIControlEventTouchDown]; 
[button4 setTitle:[theQuiz objectAtIndex:row+4] forState:UIControlStateNormal]; 
[button4 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
button4.titleLabel.lineBreakMode = UILineBreakModeWordWrap; 
button4.titleLabel.font = [UIFont systemFontOfSize:24]; 
button4.frame = CGRectMake(34, 290 + Question.frame.size.height + button.frame.size.height + button2.frame.size.height + button3.frame.size.height , 700, ans4.frame.size.height + 20); 
button4.layer.borderWidth = 3; 
button4.layer.borderColor = [[UIColor darkGrayColor ] CGColor]; 
button4.layer.cornerRadius = 10.0; 
[button4 setBackgroundImage:[UIImage imageNamed:@"buttun2.png"] forState:UIControlStateNormal]; 
button4.titleEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5); 
[self.view addSubview:button4]; 



NSString *strright = [theQuiz objectAtIndex:row+7]; 
//NSString *strtext = [[NSString alloc] initWithFormat:@"%@" , strright]; 



rightans = [[[UILabel alloc] initWithFrame:CGRectMake(50, 330, 650, 150)]autorelease]; 
rightans.numberOfLines = 0; 
rightans.text = [NSString stringWithFormat:@"%@" , strright]; 
rightans.backgroundColor = [UIColor clearColor]; 
rightans.font = [UIFont systemFontOfSize:26]; 
[rightans sizeToFit]; 
[rightans setHidden:YES]; 
[self.view addSubview:rightans]; 


//Description. 
NSString *des = [theQuiz objectAtIndex:row+6]; 
//  NSString *destext = [[NSString alloc] initWithFormat:@"\n> Explanation :\n%@\n\n" , des]; 


Description = [[[UILabel alloc] initWithFrame:CGRectMake(40, 400 + rightans.frame.size.height , 690, 150)] autorelease]; 
Description.numberOfLines = 0; 
// Description.font = [UIFont systemFontOfSize:13.5]; 
Description.text = [NSString stringWithFormat:@"\n> Explanation :\n%@\n\n" , des]; 
[Description setFont:[UIFont fontWithName:@"Futura" size:26]]; 
Description.lineBreakMode = UILineBreakModeWordWrap; 
Description.backgroundColor = [UIColor clearColor]; 
[Description sizeToFit]; 
[Description setHidden:YES]; 
[self.view addSubview:Description]; 



ContainerView = [[UIView alloc] initWithFrame:CGRectMake(30, 400 + rightans.frame.size.height , 700 ,Description.frame.size.height)]; 
ContainerView.backgroundColor = [UIColor clearColor]; 
ContainerView.layer.cornerRadius = 10.0; 
ContainerView.layer.borderColor = [[UIColor grayColor] CGColor]; 
ContainerView.layer.borderWidth = 3; 
// [ContainerView addSubview:Description]; 
[ContainerView setHidden:YES]; 
[self.view addSubview:ContainerView]; 
[ContainerView release]; 

注意一些其他点释放..。

1)变量名应该总是以一个小写字母开始。它是不是一个书面的规则,但对于自己和他人(如美国)审查它一个很好的做法,更容易。像“说明”和“问题”这样的变量名称并不好。

2)不要转贴了你的问题。如果你只格式化代码更好,你应该有你的前一个问题的答案本身..

+0

我非常感谢你的答复和工作我的代码..但我以前的问题是关于内存泄漏..它被解决..在这里没有内存泄漏,但仍然我的应用程序崩溃.. – iUser

+1

在您发布您的代码是释放自动释放的对象,这不是你想要的。并且此代码会因为您释放对象而崩溃。要么删除autorelease,要么不要释放标签。 – rckoenes

+0

感谢@rckoenes注意到..我从问题中复制代码,但没有注意到他正在使用autorelease .. – Krishnabhadra