如何使用产品>释放对象在XCode中进行分析? [英] How to release objects using Product > Analyze in XCode?

查看:39
本文介绍了如何使用产品>释放对象在XCode中进行分析?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅以下代码,它只是一个方法,我无法理解其中的内存管理问题,请告诉我,以下代码中发生了什么,为什么?

See the following code, it is simply a method, and I am unable to understand the memory management issues in it, let me know, what is happening in the following code, and why?

-(IBAction)selectMarina {
NSLog(@"Select Marina");
actionSheet = [[UIActionSheet alloc] initWithTitle:nil 
                                          delegate:nil
                                 cancelButtonTitle:nil
                            destructiveButtonTitle:nil
                                 otherButtonTitles:nil];

[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];    
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;

[actionSheet addSubview:pickerView];
//[pickerView release];

UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Close"]];
closeButton.momentary = YES; 
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:@selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:closeButton];
//[closeButton release];

[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
[pickerView selectRow:0 inComponent:0 animated:NO];

}

这里是说在第112行分配并存储到关闭按钮中的对象的潜在泄漏

它在行

    [actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];

如何在此代码中进行适当的内存管理,您能否详细指定它

How can I do proper memory management in this code, and can you specify it in details

谢谢

推荐答案

首先,您需要释放实例pickerView和closeButton.

First of all you need to release both the instance pickerView and closeButton.

按照规则,每当您分配对象时,作业完成后就必须释放该对象.在您的情况下,不需要将两个实例都添加到子视图中,因此必须将其释放.否则,它将显示您提到的泄漏.

As per rule whenever u alloc an object it has to be released once it job is completed. In your case after adding both the instance to the subview are not required so they has to released. Otherwise it will show the leak as you mentioned.

要了解更多信息,请参阅内存指南或快速浏览一下http://www.raywenderlich.com/2657/memory-management-in-objective-c-tutorial

For more understanding refer memory guide or for a quick look click http://www.raywenderlich.com/2657/memory-management-in-objective-c-tutorial

这篇关于如何使用产品>释放对象在XCode中进行分析?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆