简体   繁体   English

目标中的内存管理-c

[英]Memory management in objective-c

I have this code in one of my classes: 我在我的一个课程中有这个代码:

- (void) processArray
{

   NSMutableArray* array = [self getArray];
   . . .
   [array release];
   array  = nil;
}


- (NSMutableArray*) getArray
{
   //NO 1:
   NSMutableArray* array = [[NSMutableArray alloc]init];

   //NO 2:
   NSMutableArray* array = [NSMutableArray array];
   . . .
   return array;
}

NO 1: I create an array and return it. NO 1:我创建一个数组并将其返回。 In the processArray method I release it. processArray方法中我释放它。

NO 2: I get an array by simply calling array . NO 2:我通过简单地调用array得到一个array As I'm not owner of this, I don't need to release it in the processArray method. 因为我不是这个的拥有者,所以我不需要在processArray方法中释放它。

Which is the best alternative, NO 1 or NO 2? 哪个是最佳选择,NO 1还是NO 2? Or is there a better solution for this? 或者有更好的解决方案吗?

The method should return an autoreleased array, NO 2 is the better choice. 该方法应该返回一个自动释放的数组,NO 2是更好的选择。 The reason for this is that when you call the method with 原因是当你用方法调用方法时

   NSMutableArray* array = [self getArray];

you will expect as a matter of convention to not have to release the array. 作为惯例,你会期望不必释放数组。 If you want to take ownership of it, you retain it, but otherwise you shouldn't have to care about it. 如果你想拥有它,你保留它,但否则你不应该关心它。 In Cocoa, you only take ownership by explicitly sending alloc or retain or copy (or new ). 在Cocoa中,您只能通过显式发送allocretaincopy (或new )来获取所有权。 Since the processArray method doesn't do that, it shouldn't have to take care of releasing the array. 由于processArray方法不这样做,因此不必处理释放数组的问题。

So you should use NO 2, and also you should remove these two lines: 所以你应该使用NO 2,你也应该删除这两行:

[array release];
array  = nil;

If the array and its contents use a lot of memory or its used lots of times, you'll want to release them straight away, so use option 1. According to the Objective-C guidelines, you'll want to prefix the word "new" to your subroutine name instead of "get" in that case. 如果数组及其内容使用了大量内存或其使用了很多次,您将需要立即释放它们,因此请使用选项1.根据Objective-C指南,您需要在单词“前缀”前加上在这种情况下,新的“到您的子程序名称而不是”获取“。

If on the other hand, you want to reduce the number of lines of code that say simply [array release]; 另一方面,如果你想减少简单地说[array release]的代码行数; or similar then use option 2. 或类似然后使用选项2。

It is simply a balance between reducing lines of code, and reducing unnecessary temporary memory use. 它只是减少代码行和减少不必要的临时内存使用之间的平衡。

Whilst the autorelease pool will help in reducing memory leaks and make your code smaller, sometimes you need to explicitly release everything as it goes out of use to keep the use of memory down. 虽然自动释放池将有助于减少内存泄漏并使代码更小,但有时您需要明确释放所有内容,因为它不再使用以保持内存的使用。

HTH HTH

EDIT 编辑

Ah - I stand corrected. 啊 - 我的立场得到了纠正。 Reading the iPhone version of the Memory Management Programming Guide for Cocoa I see that the iPhone guidelines are to use a prefix of "new..." so for example "newArray" in this case, if the caller is supposed to manually release and NOT a prefix of "create...". 阅读适用于Cocoa的内存管理编程指南iPhone版本我看到iPhone指南使用的是“new ...”的前缀,例如“newArray”在这种情况下,如果调用者应该手动释放而不是前缀“创建...”。 "Creating" can refer either to creation of manually released or of automatically released objects and so would be ambiguous. “创建”可以指创建手动释放或自动释放的对象,因此可能不明确。 Text corrected above. 上面纠正了文字。

- (void) processArray
{

   NSMutableArray* array = [[self getArray] retain];
   //Now you are the owner of array, so you take care to release it

   . . .

   [array release];
   array  = nil;
}


- (NSMutableArray*) getArray
{
   //create a new array 
   //temporarily the method owns the array
   NSMutableArray* array = [[NSMutableArray alloc]init];

   //fill in here with elements or what you want
   ..........

   [array autorelease];
   //autorelease here says "I don't own the result
   //if anyone cares about it, he should retain it himself

   return array;
}

So in short when you create new objects you should autorelease them before returning. 因此,在创建新对象时,您应该在返回之前自动释放它们。 Because if the calling method wants to use the result, the calling method should take care of retaining and releasing the result. 因为如果调用方法想要使用结果,则调用方法应该保留并释放结果。

It's always good to run the Klang static analyzer for this issues, when you are not really sure in your retaining/releasing code : http://clang-analyzer.llvm.org/ 当你在保留/发布代码中不确定时,为这个问题运行Klang静态分析器总是好的: http//clang-analyzer.llvm.org/

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

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