EXC_BAD_ACCESS错误,内存管理问题

时间:2011-05-27 01:49:40

标签: iphone objective-c xcode memory-management

我已经读过这个错误来自内存管理问题,例如向已发布的对象发送消息。我在评论“输出有关'info'数组中的歌曲的信息”之后,在第二个'for'部分的第一行中收到错误。 info数组是否没有在第一个'for'部分中存储我提供的对象?

还有其他想法吗?

MPMediaQuery *query = [[MPMediaQuery alloc] init]; //query iPod library
NSMutableArray *info; //create array to hold songs that fit
NSArray *allSongs = [query collections];

//only add those songs which have not
//been played since last login

for (MPMediaItem *recent in allSongs) {
    NSDate *lastPlayed = [recent valueForProperty:MPMediaItemPropertyLastPlayedDate];
    BOOL uploadInfo = [[PlayedSinceLastLogin alloc] initWithLastPlayedDateOfSong:lastPlayed] ;
    if (uploadInfo == YES) {
        [info addObject:recent];
    }
}

//output information about songs
//in the 'info' array

for (MPMediaItem *played in info) {
    NSString *playCount = [played valueForProperty:MPMediaItemPropertyPlayCount];
    NSString *lastPlayed = [played valueForProperty:MPMediaItemPropertyLastPlayedDate];
    NSString *songTitle =
    [played valueForProperty: MPMediaItemPropertyTitle];
    NSString *artistName =
    [played valueForProperty: MPMediaItemPropertyArtist];
    NSString *albumName =
    [played valueForProperty: MPMediaItemPropertyAlbumTitle];
    NSLog (@"\n %@ by %@, from album %@, played since last login.\nLast Played:%@.\nTotal Play Count: %@.", songTitle, artistName, albumName, lastPlayed,playCount);

}

1 个答案:

答案 0 :(得分:3)

看起来您实际上并没有为NSMutableArray创建info的实例来指向,因此错误的访问错误可能是由于尝试处理某些错误随机的内存位作为初始化对象。

更改

NSMutableArray *info;

NSMutableArray *info = [NSMutableArray array];