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?

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Is it really necessary!! Yes you are right. Because when first I am just autoreleasing destArray.Not retaining It causes the crashes.
@Adhira: I have setted properties and synthesise then also I need to write retain because otherwise it will contain autoreleased objects...
Is good that you write him the code. But it will also be nice if he will understand why the code is like you written it and not like he was doing it.
@ArpitParekh, before retaining another array in shuffle method, I am releasing the earlier retained array. See my Edit.
But if I release it how can I use it to shuffle again..!!?
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

3 Comments

Hey but when should I release array because the method caontains the same array
When I change the refrence, where should I release it.
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];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.