0

There are multiple memory leaks in this section of my code. Specifically with these arrays: PlaylistItem, PlaylistItemID and PlaylistItemLength. The problem is that I can't successfully release the arrays. When I attempt to use insert [xxxx release]; anywhere in this code, the app locks up. It's driving me absolutely nurtz!

-(void)configureCueSet {
MPMediaQuery *myPlaylistsQuery = [MPMediaQuery playlistsQuery];
NSArray *playlists = [myPlaylistsQuery collections];

//Get # of items in a playlist and names -------------------------------------
NSArray *songs;
for (MPMediaPlaylist *playlist in playlists) {
    NSString *playListItem = [playlist valueForProperty: MPMediaPlaylistPropertyName];
    if ([playListItem isEqualToString: savedLastSelectedPlaylist]){
        songs = [playlist items];
    }
}
PlaylistItem = [[NSMutableArray alloc] init];
PlaylistItemID = [[NSMutableArray alloc] init];
PlaylistItemLength = [[NSMutableArray alloc] init];
for (MPMediaItem *song in songs) {
    [PlaylistItem addObject:[song valueForProperty: MPMediaItemPropertyTitle]];
    [PlaylistItemID addObject:[song valueForProperty: MPMediaItemPropertyPersistentID]];
    [PlaylistItemLength addObject:[song valueForProperty: MPMediaItemPropertyPlaybackDuration]];
}
}

2 Answers 2

1

Does that method get called multiple times? If so, your leak likely occurs on that assignment. You'd want:

[PlayListItem release];
PlaylistItem = [[NSMutableArray alloc] init];

[PlayListItemID release];
PlaylistItemID = [[NSMutableArray alloc] init];

[PlaylistItemLength release];
PlaylistItemLength = [[NSMutableArray alloc] init];

If you don't release what was there before, then you'll get a leak.

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

2 Comments

Thanks Nick! That did the trick! It makes such obvious sense now. The method IS called multiple times. Sometimes I can't see the forest for the trees!
Hey, no problem. Glad I could help. Watch out for the bears. ;)
1

Attempting to insert [xxx release] would release the contents, not the arrays. The application crashes because with that you are deallocating the object which you are about to add to the array. According to the documentation (here), the values in an NSArray are automatically retained, and will be released as soon as the array is dealloc'ed. So, if you want to release any of those arrays, simply type [PlaylistItem release].

5 Comments

Thanks for the reply. Adding [PlayListItem release]; produces "SIGABRT". I am baffled.
Where are you adding it? You should only release the arrays when you are sure that you dont need them any more. As far as your codesnippet goes, you are filling the arrays without actually using them. Releasing them somewhere there would make no sense.
The array is used in multiple places in the app. So I just put up with the leaks until I am no longer using the arrays? Won't my app be rejected for the leaks?
When you add objects to PlayListItem their retain count is increased. Later when you release the array, all object's retain count is decreased. So if these objects were initially placed in the array with retain=1 but autoreleased (and this is the case with the [song valueForProperty:xxxx]) once the array is released these objects will be disposed from memory and any future reference somewhere else in the code will crash the app. Why don't you use NSZombieEnabled signaling to understand which are the objects released and then referenced?
Also, keeping arrays you still need is not "leaky". A leak is not releasing memory that you are no longer using.

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.