Chris
Chris

Reputation: 12181

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

}

Upvotes: 3

Views: 199

Answers (1)

Seamus Campbell
Seamus Campbell

Reputation: 17916

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

Upvotes: 3

Related Questions