2011-05-27 93 views
3

我讀過這個錯誤來自內存管理問題,如發送消息,該消息已被釋放的對象的內存管理問題。我在'評論'輸出有關'信息'數組中的歌曲'的信息之後,在'第二部分的第一行'中找到錯誤。 info數組沒有存儲我在第一個'for'部分給出的對象嗎?EXEC_BAD_ACCESS錯誤,

還有其他想法嗎?

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

} 

回答

3

它看起來並不像你曾經實際創建的NSMutableArray一個實例info指向,所以壞訪問錯誤可能是由於試圖把一些內存隨機位作爲一個初始化的對象。

變化

NSMutableArray *info; 

NSMutableArray *info = [NSMutableArray array]; 
+1

作爲一個側面說明,如果你真的偏執,這是一個好主意,如果你還沒有準備好分配一個值零,初始化: NSMutableArray * info = nil; – makdad 2011-05-27 01:55:35

+0

非常好!我認爲這個方法從a - 到a +之前是以前的一個ivar。非常感謝! – Chris 2011-05-27 02:42:11

+0

我也必須加註UITextView對象的實例嗎?當我嘗試將textView.Text更改爲我剛製作的字符串時,出現類似的錯誤。 – Chris 2011-05-27 02:50:21

相關問題