1

In my iOS app, I am using a NSMutableArray, named imageMArray. I have set its getter and setter properties and instantiated it.

In viewDidLoad:

imageMArray=[[NSMutableArray alloc] initWithArray:CategoryImages];
imageMArray=[self shuffleOnlyArray:imageMArray];

In ShuffleOnlyArray Method:

 NSMutableArray *destArray1 = [[NSMutableArray alloc] initWithCapacity: [sourceArray count]] ;
return destArray1;

In shuffle Method:

imageMArray=[[self shuffleOnlyArray:imageMArray] retain];

There appears to be a memory leak in the Shuffle method.

Should I release imageMArray or set it to nil? If it should be released, should it be autoreleased?

Vemonus
  • 868
  • 1
  • 16
  • 28
Arpit B Parekh
  • 1,932
  • 5
  • 36
  • 57

2 Answers2

5
imageMArray=[[NSMutableArray alloc] initWithArray:CategoryImages];

In the above statement, you have a memoryleak. Instead you can have like as follows.

imageMArray = [NSMutableArray arrayWithArray:CategoryImages];

In ShuffleOnlyArray Method, return the autoreleased object.

NSMutableArray *destArray1 = [[NSMutableArray alloc] initWithCapacity: [sourceArray count]] ;
return [destArray1 autorelease];

But after you get it, retain (take the ownership) the array object.

imageMArray=[[self shuffleOnlyArray:imageMArray] retain];

Edit

In shuffle method, do as follows:

NSMutableArray *imageMArray1 = [imageMArray mutableCopy];
if( imageMArray )
{
   [imageMArray release];
}
imageMArray=[[self shuffleOnlyArray:imageMArray1] retain];
[imageMArray1 release];

Edit 2: One more solution:

Use the category to shuffle as mentioned in the SO link

No need of creating new and releasing the arrays.

Community
  • 1
  • 1
Ilanchezhian
  • 17,426
  • 1
  • 53
  • 55
1

1 You already have a memory leak in the following lines.

imageMArray = [[NSMutableArray alloc] initWithArray:CategoryImages];

imageMArray = [self shuffleOnlyArray:imageMArray];

In the first line you create an object with retain count 1. Then you say that your imageMArray pointer points to other object. You should release the first object, because you louse the reference to the fist object and you can not release it after you change the reference!

2 You should not use retain because your ShuffleOnlyArray method returns a retained object. Your factory method should return an autorelease object and the caller of the factory should decide if if will retain it or not.

Hope I was clear enough

Alex Terente
  • 12,006
  • 5
  • 51
  • 71
  • Hey but when should I release array because the method caontains the same array – Arpit B Parekh Mar 16 '12 at 07:34
  • When I change the refrence, where should I release it. – Arpit B Parekh Mar 16 '12 at 08:49
  • You can make the array autorelease as Aadhira did or you just call. [imageMArray release]; and then imageMarray = [self shuffleOnlyArray:imageMArray];. And take in mind that shuffleOnlyArray have to return auto relsease and you have to retain as Aadhira did imageMArray=[[self shuffleOnlyArray:imageMArray] retain]; – Alex Terente Mar 16 '12 at 10:54