开发者

NSMutableArray Memory Leak Issue

开发者 https://www.devze.com 2023-02-08 20:56 出处:网络
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 a

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 val开发者_Python百科ueForProperty: MPMediaItemPropertyPersistentID]];
    [PlaylistItemLength addObject:[song valueForProperty: MPMediaItemPropertyPlaybackDuration]];
}
}


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.


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].

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号