iphone - EXC_BAD_ACCESS 错误,内存管理问题

标签 iphone objective-c xcode memory-management

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

}

最佳答案

看起来你实际上并没有为 info 创建一个 NSMutableArray 的实例来指向,所以错误的访问错误可能是由于试图将一些随机的内存位视为已初始化的对象。

改变

NSMutableArray *info;

NSMutableArray *info = [NSMutableArray array];

关于iphone - EXC_BAD_ACCESS 错误,内存管理问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6147036/

相关文章:

objective-c - 将十六进制值从字符串转换为十六进制颜色代码

iphone - 核心图 CPT ScatterPlot 'Line Graph' 未显示

ios - 容器 View 和传递数据

iphone - SQLite3 数据库未通过 NSLog 在控制台上显示条目(将数据保存到数据库有效)

ios - 用于 Unwind Segue 的自定义 Segue 类

iphone - 具有匿名函数/ block 的 NSTimer?

iphone - 如何移动 UITextField 中的清除按钮?

ios - 点击后 UitableView 改变 sectionView

swift - 如何更改 DispatchTimeInterval 以获得成员毫秒

iphone - genstrings 工具抛出异常,在解析 XCode 项目时中止