EXEC_BAD_ACCESS错误,内存管理问题

发布于 2024-11-09 17:11:11 字数 1377 浏览 0 评论 0原文

我读到这个错误来自内存管理问题,例如向已释放的对象发送消息。我在第二个“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);

}

I've read that this error comes from memory-management issues such as sending a message to an object which has been released. 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. Is the info array not storing the objects that I am giving it in the first 'for' section?

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

屌丝范 2024-11-16 17:11:11

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

更改

NSMutableArray *info;

NSMutableArray *info = [NSMutableArray array];

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.

Change

NSMutableArray *info;

to

NSMutableArray *info = [NSMutableArray array];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文