2011-08-18 78 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