tags:

views:

37

answers:

2

Hi,

I have an NSMutableArray called playlist. This is in a method called getAllPlaylists. The code is something like this:

-(NSMutableArray *)getAllPlaylists
{
    //playlist is an instance variable
     playlist = [[NSMutableArray alloc] init]; //memory leak here
    ...
    //some code here which populates the playlist array
    [playlist addObject: object1];
    ...
     return playlist;
}

The array allocation step of playlist is causing a memory leak. In such a scenario where can i release this array? Or can i avoid allocation n initialization of playlist here by doing something else? Any help will be greatly appreciated!!

+1  A: 

2 solutions:

Use autorelease:

- (NSMutableArray*)getAllPlaylists
{
    playlist = [[NSMutableArray alloc] init];
    ...
    return [playlist autorelease];
}

or instead of using [[NSMutableArray alloc] init] to create your NSMutableArray object, use [NSMutableArray array] which is equivalent to [[[NSMutableArray alloc] init] autorelease]:

- (NSMutableArray*)getAllPlaylists
{
    playlist = [NSMutableArray array];
    ...
    return playlist;
}
macatomy
Thanks!! But the last time i tried playlist = [NSMutableArray array]; the app crashed. Anyways, i will try it once again.
ulag
This will work as long as you don't ever try using `playlist` in any other methods in your class. In this case, it wouldn't make sense to have an instance variable at all (depends on what you're trying to do).
wbyoung
If you use `playlist = [NSMutableArray array];` make sure you don't attempt to release it anywhere. That will likely result in a crash.
macatomy
I used playlist = [NSMutableArray array];However, I was returning the playlist array and storing it in another NSMutableArray. The reason, why it crashed the first time i set playlist to autorelease, was that i did not retain the NSMutableArray to which i received the playlist array. Once i retained it and later released it using dealloc, the code worked fine.Thanks a lot for your help guys!!
ulag
+1  A: 

You should autorelease newly created objects that you want to return that are not owned by the object (local variables, not instance variables).

playlist = [[[NSMutableArray alloc] init] autorelease];

Alternatively, you can use the convenience method to do that more easily:

playlist = [NSMutableArray array];

For items the object owns (instance variables), you should make sure you release the old value first and implement a dealloc method that also releases the value.

- (NSMutableArray*)getAllPlaylists {
    [playlist release];
    playlist = [[NSMutableArray alloc] init];
    return playlist;
}

- (void)dealloc {
    [playlist release];
    [super dealloc];
}

For more info, see the memory management guide.

wbyoung
Thanks!! But, the first time when getAllPlaylists is called, the playlist array will not have any memory allocation to release. Wont that cause an error?
ulag
The first time `getAllPlaylists` is called, `playlist` will be `nil`. Sending a message to `nil` [is allowed](http://developer.apple.com/mac/library/documentation/cocoa/conceptual/objectivec/Articles/ocObjectsClasses.html#//apple_ref/doc/uid/TP30001163-CH11-SW7) in Objective-C.
wbyoung
Thanks, i will try this.
ulag
Read that [memory management guide](http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html) over and over. Even if you don't understand it the first time, spend some time coding, then come back to it. Eventually it will click.
wbyoung