简体   繁体   English

EXEC_BAD_ACCESS 错误,memory 管理问题

[英]EXEC_BAD_ACCESS error, memory management issues

I've read that this error comes from memory-management issues such as sending a message to an object which has been released.我读到此错误来自内存管理问题,例如向已发布的 object 发送消息。 I am getting the error right after the comment "output information about songs in the 'info' array", in the first line of that second 'for' section.在第二个“for”部分的第一行中,我在评论“输出有关 'info' 数组中歌曲的信息”之后立即收到错误消息。 Is the info array not storing the objects that I am giving it in the first 'for' section?信息数组是否没有存储我在第一个“for”部分中提供的对象?

Any other ideas?还有其他想法吗?

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);

}

It doesn't look like you've ever actually created an instance of NSMutableArray for info to point to, so the bad access error is probably due to attempting to treat some random bit of memory as an initialized object.看起来您实际上从未创建过NSMutableArray的实例以供info指向,因此错误的访问错误可能是由于试图将 memory 的一些随机位视为初始化的 object。

Change改变

NSMutableArray *info;

to

NSMutableArray *info = [NSMutableArray array];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM