получено предупреждение памяти. 1-й уровень - PullRequest
0 голосов
/ 17 августа 2011

Я новичок в разработке для iPhone. Я создаю приложение, в котором мне нужно создавать кнопки и метки программно.
Поэтому я делаю это в .h файле

@interface FirstQuizViewControlleriPad : UIViewController{

IBOutlet UIButton *startgame;
UILabel *theQuestion;
IBOutlet UIScrollView *scrollview;
UILabel *Question;
UILabel *Description;
UIView *ContainerView;
UILabel *Challengetext;
UIImageView *img1;
UIImageView *img2;
UIImageView *background;
IBOutlet UIButton *reviewbtn;
UILabel *ans1;
UILabel *ans2;
UILabel *ans3;
UILabel *ans4;

UIButton *button;
UIButton *button2;
UIButton *button3;
UIButton *button4;
UILabel *rightans;
UILabel *theScore;
UIButton *nextbtn;
UIButton *backbtn;
NSString *setchallange;

AVAudioPlayer *AVback;
AVAudioPlayer *AVstart;
NSArray *theQuiz;
NSInteger questionNumber;
NSInteger rightAnswer;
NSInteger myScore;
NSInteger finalScoreipad1;
BOOL restartGame;
id QuestionReview;

}

@property (retain , nonatomic) IBOutlet UIButton *startgame;
@property (retain , nonatomic) NSArray *theQuiz;
@property (retain , nonatomic) IBOutlet UILabel *theScore;
@property (retain , nonatomic) IBOutlet UIButton *nextbtn;
@property (retain , nonatomic) IBOutlet UIScrollView *scrollview;
@property (retain , nonatomic) IBOutlet UIButton *backbtn;
@property (retain , nonatomic) IBOutlet UILabel *Challengetext;
@property (retain , nonatomic) IBOutlet UIImageView *img1;
@property (retain , nonatomic) IBOutlet UIImageView *img2;
@property (retain , nonatomic) id QuestionReview;
@property (retain , nonatomic) IBOutlet UIButton *reviewbtn;
@property (retain ,nonatomic) IBOutlet UIButton *button;
@property (retain ,nonatomic) IBOutlet UIButton *button2;
@property (retain ,nonatomic) IBOutlet UIButton *button3;
@property (retain ,nonatomic) IBOutlet UIButton *button4;
@property (retain ,nonatomic) IBOutlet UILabel *ans1;
@property (retain ,nonatomic) IBOutlet UILabel *ans2;
@property (retain ,nonatomic) IBOutlet UILabel *ans3;
@property (retain ,nonatomic) IBOutlet UILabel *ans4;
@property (retain , nonatomic) IBOutlet UILabel *Question;
@property (retain , nonatomic) NSString *setchallange;

-(void)askQuestion;

в .m файле

questionNumber = questionNumber + 1;
NSInteger row = 0;
if(questionNumber == 1)
{
    row = questionNumber - 1;
}
else
{
    row = ((questionNumber - 1) * 10);
}

NSString *selected = [theQuiz objectAtIndex:row];
NSString *activeQuestion = [[NSString alloc] initWithFormat:@"%@", selected];
QuestionReview = activeQuestion;

//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 = activeQuestion;
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)];
rightans.numberOfLines = 0;
rightans.text = strtext;
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 = destext;
[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];

//For Challenge Text
NSString *challange = [theQuiz objectAtIndex:row+8];
  setchallange = [[NSString alloc] initWithFormat:@"%@" , challange];//Memory leak here
 Challengetext.text = setchallange;



//Image For Every Challenge
background = [[[UIImageView alloc] initWithFrame:CGRectMake(15.0f, 15.0f, 130.0f, 110.0f)] autorelease];
[background setImage:[UIImage imageNamed:[theQuiz objectAtIndex: row + 9]]];
[self.view addSubview:background];

В функции dealloc я освобождаю все метки, кнопки и другие объекты, которые я создал, но все же я получил это предупреждение об утечке памяти (получено предупреждение об уровне памяти 1 и иногда уровень 2)
Я искал, где у меня просачивается память, и это утечка в NSString setchallenge

Пожалуйста, помогите мне это ..
Любая помощь будет оценена ..

Спасибо ..

Ответы [ 3 ]

1 голос
/ 17 августа 2011

Вы создаете новую строку при каждом вызове setchallenge = [[NSString alloc] ....

Должно быть:

//For Challenge Text
NSString *challange = [theQuiz objectAtIndex:row+8];
Challengetext.text = [NSString stringWithFormat:@"%@" , challange];

stringWithFormat возвращает автоматически высвобождаемую строку, которая будет освобождена при следующем сливе пула автоматического освобождения.

Джо

1 голос
/ 17 августа 2011

Поскольку вы распределили память, вам также необходимо освободить ее. Каждый раз, когда вы используете alloc, copy или new, не забывайте всегда выпускать. Не стоит выпускать все только на dealloc. Выпускать объекты, когда вы закончите с ними. Например:

 setchallange = [[NSString alloc] initWithFormat:@"%@" , challange];//Memory leak here
 Challengetext.text = setchallange;
 [setchallange release]

Также ознакомьтесь с Руководством по управлению памятью iOS . Я уверен, что это поможет.

0 голосов
/ 17 августа 2011

Может быть для некоторого объекта, который вы выделяете более одного раза, и отпустите его один раз. просто проверьте это, я надеюсь, что это решит.

...