简体   繁体   中英

How to release the locally alloc init NSMutableArray and return its reference

I have one function like this

    -(NSMutableArray *)getData
    {
       NSMutableArray *tempArr=[NSMutableArray alloc]init];
      // perform some operation on tempArr
      return tempArr;
    }

In above example i created new object for nsmutablearray and need to return its refernce. Now everything is working fine however when i run it with instruments (leaks) it shows 100 % memory leak at "NSMutableArray *tempArr=[NSMutableArray alloc]init];" this line.

what can be solution for this,please do write to this thread

Thnx in advce

I usually do this:

return [tempArr autorelease];

Consider switching to ARC.

For such cases you should use autorelease like this...

 -(NSMutableArray *)getData
    {
       NSMutableArray *tempArr=[[NSMutableArray alloc]init]autorelease];
      // perform some operation on tempArr
      return tempArr;
    }

hoping this helps.

You will have to release it on return, like so

 -(NSMutableArray *)getData
    {
       NSMutableArray *tempArr=[NSMutableArray alloc]init];
      // perform some operation on tempArr
      return [tempArr autorelease];
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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