Objective c NSMutableArray内存泄漏

Objective c NSMutableArray内存泄漏,objective-c,Objective C,XCode正在报告特定代码行上的内存泄漏: (NSArray*)myFunction{ NSMutableArray * tempMapListings=[[NSMutableArray alloc] init]; //Xcode says leak is here //do a bunch of stuff to insert objects into this mutable array return tempMapListings; [tempMapLis

XCode正在报告特定代码行上的内存泄漏:

(NSArray*)myFunction{
   NSMutableArray * tempMapListings=[[NSMutableArray alloc] init]; //Xcode says leak is here

   //do a bunch of stuff to insert objects into this mutable array


    return tempMapListings;
    [tempMapListings release]; // but I release it ?!

   }

这是因为作为NSArray发布了可变数组吗?由于mutable继承自inmutable,因此我认为这不是问题,而且无论如何,对象都会被释放。我非常感谢第二只眼的建议。

不,你不会发布它的。return语句实际上在该点结束了方法的执行。那么,在你的情况下,它下面的线

[tempMapListings release]; // but I release it ?!
没有执行

而是使用自动释放:

您可以在许多地方了解自动释放。在苹果自己的文档中查找它;您也可以用谷歌搜索它。

您从函数返回后将发布tempMapListings。在return语句之后,不再在该分支上执行代码。因此,您的[tempListListings release]语句永远不会运行。此外,当您返回它时,您实际上并不想立即释放它-调用者将永远没有机会保留数组

自动释放池是你的朋友。添加到自动释放池中的对象最终会代表您释放,给您的调用者时间来获取结果。要将对象添加到默认池,请将分配行更改为

NSMutableArray *tempMapListings = [[[NSMutableArray alloc] init] autorelease];
并删除最后的释放调用


有关自动释放池的更多信息,请阅读。它们真的非常有用。

这是因为作为NSArray发布了可变数组吗?-你这是什么意思?问题不在这里,但它与该功能有关。你能发布所有使用该函数的代码吗?你能准确地展示一下你对这个阵列做了什么吗?
NSMutableArray *tempMapListings = [[[NSMutableArray alloc] init] autorelease];