简体   繁体   English

什么时候释放整个类中使用的对象?

[英]when to release object that is used throughout the class?

I know that the basic rule of memory management in objective-C is that for every object you do an alloc, you need to release it somewhere. 我知道Objective-C中内存管理的基本规则是,对于每个执行分配的对象,都需要将其释放到某个地方。

Say I have the following: 说我有以下几点:

@interface NewPollViewController : UIViewController 
{
   NSMutableArray * myArray;
}

@property (nonatomic, retain) NSMutableArray * myArray;

@implementation NewPollViewController
@synthesize myArray = _myArray;

- (void) viewDidLoad
{
  [super viewDidLoad];
  self.myArray = [[NSMutableArray alloc] init];
}

Say that this self.myArray is used as a dataSource of a UITableView, so when and where should I release this? 假设此self.myArray用作UITableView的数据源,那么我应在何时何地发布此文件? In the dealloc? 在dealloc中? I guess that the release in the dealloc corresponds to the retain that I've set in my property, so where do I release 我猜想dealloc中的发布对应于我在属性中设置的保留,因此我应该在哪里发布

In your example you technically need to release it twice - once in dealloc , and once immediately after setting the property: 在您的示例中,从技术上讲,您需要释放两次-一次在dealloc ,一次在设置属性后立即释放:

NSMutableArray *a = [[NSMutableArray alloc] init];
self.myArray = a;
[a release];

The reasoning for this is because you are specifically allocating memory in viewDidLoad , and then also increasing the retain count when setting the property. 这样做的原因是因为您要专门在viewDidLoad分配内存,然后在设置属性时增加保留计数。

A way to avoid this is to use one of the static NSMutableArray constructors or use autorelease ie 避免这种情况的一种方法是使用静态NSMutableArray构造函数之一或使用autorelease

self.myArray = [[[NSMutableArray alloc] init] autorelease];

Alternatively, bypass the property altogether: 或者,完全绕过该属性:

myArray = [[NSMutableArray alloc] init];

This would avoid the extra retain generated by the property (in fact you could get rid of the property statement if it was only used locally). 这样可以避免属性产生的额外保留(实际上,如果仅在本地使用property语句,则可以摆脱它)。

If I want to expose a NSMutableArray I would do this: 如果我想公开一个NSMutableArray,我可以这样做:

@interface NewPollViewController : UIViewController 
{
   NSMutableArray * myArray;
}

@property (nonatomic, readonly) NSMutableArray * myArray;

@implementation NewPollViewController
@synthesize myArray;

- (void) viewDidLoad
{
  [super viewDidLoad];
  self.myArray = [[NSMutableArray alloc] init];
}

- (void) dealloc
{
   [myArray release],myArray = nil;
}

Changed the property to readonly because its mutable array you don't want other classes to change this array and you are properly alloc and releasing it. 将该属性更改为只读,因为它的可变数组您不希望其他类更改此数组,并且可以正确分配和释放它。

In your case I would often release it in the viewDidUnload , and also in dealloc . 在您的情况下,我通常会在viewDidUnloaddealloc释放它。

I like to maintain a mirror with respect to where the memory is allocated. 我喜欢维护有关内存分配位置的镜像。 When its done in viewWillAppear , I release in viewWillDisappear . 当它在viewWillAppear完成时,我在viewWillDisappear释放。

Now since you are saying this is used thru-out the class, I would allocate in an init method, or the awakeFromNib , and then just release it once in dealloc . 现在,既然您说的是整个类都使用了它,那么我将在init方法或awakeFromNib中进行分配,然后在dealloc中将其释放一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM