1

I have tried to find answers online and I did find a few relevant answers on SO, but I really want to confirm I understand it correctly (which maybe I don't)

I have this code:

- (NSArray*)arrayMethod
{
MPMediaQuery *albumArtistsQuery = [MPMediaQuery artistsQuery];
[albumArtistsQuery setGroupingType:MPMediaGroupingAlbumArtist];

return @[
    @[ kTypeArtists, NSLocalizedString(@"Artists", nil), albumArtistsQuery, @"skin:iconArtists" ],
    @[ kTypeAlbums, NSLocalizedString(@"Albums", nil), [MPMediaQuery albumsQuery], @"skin:iconAlbums" ],
    @[ kTypeTracks, NSLocalizedString(@"Tracks", nil), [MPMediaQuery songsQuery], @"skin:iconSongs" ],
    @[ kTypeComposers, NSLocalizedString(@"Composers", nil), [MPMediaQuery composersQuery], @"skin:iconComposers" ],
    @[ kTypeGenres, NSLocalizedString(@"Genres", nil), [MPMediaQuery genresQuery], @"skin:iconGenres" ],
    @[ kTypePlaylists, NSLocalizedString(@"Playlists", nil), [MPMediaQuery playlistsQuery], @"skin:iconPlaylists" ],
    @[ kTypePodcasts, NSLocalizedString(@"Podcasts", nil), [MPMediaQuery podcastsQuery], @"skin:iconPodcasts" ],
];
}

- (void)someMethod
{
    NSMutableDictionary* dict = [NSMutableDictionary dictionary];    
    for (NSArray* current in [self arrayMethod]) {
        [dict setObject:current forKey:current[0]];
    }
    self.rootDict = [NSDictionary dictionaryWithDictionary:dict];
}

kTypeArtists etc are just static const NSString*

When I use profiling instruments (more specifically leak instrument) in xcode, I get a memory leak of the returned NSArray and the responsible frame is the method arrayMethod

From what I've read my understanding is, that this is not a memory leak, but when inside someMethod I'm passing the NSArray to otherMethod as an argument, retain count is increased for the array and will be released some time later in the future when autorelease pool drains, but this can happen later in runtime, so it's not really a memory leak, just the leak instrument shows it as such;

But I'm also skeptical, that the tool would be showing it as memory leak, then it would be quite useless tool; So maybe my understanding is not correct and I actually do have a memory leak, but I just can't understand why.

Can anybody explain if I'm right or wrong and why? Thanks!

EDIT: I have also tried to change the code to [[self arrayMethod] copy] but didn't help;

Lukas1
  • 582
  • 1
  • 5
  • 28
  • 1
    I cannot reproduce your problem. Please update OP with the code you use to "*iterate over array do nothing else with it*". You are completely right when you say this shouldn't be causing a leak, I can only assume it's due to something else you're doing with the array, or a problem with Instruments (try cleaning your build folder & restarting your computer). – Hamish Feb 10 '16 at 10:28
  • 2
    Also, sounds silly, but make sure you're *actually* using ARC... Go into your **Build Settings > Language - Objective C > Objective-C Automatic Reference Counting**. Make sure it's turned on for your target build settings. – Hamish Feb 10 '16 at 10:41
  • @originaluser2 thank you for your comments; I have verified that I am indeed using ARC; (I guess the app would be anyway crashing if not ;o) ). I have updated the code in question so that it much more closely resembles what I have; I have tried to migrate this code as well to a separate project and see if I can reproduce the memory leak, but in the other project there was no memory leak, interestingly enough; – Lukas1 Feb 10 '16 at 10:59
  • The leak instrument would have me believe that the problem is in the `arrayMethod` but clearly that can't be the case :-( So not sure what is wrong here at all; Note: (if you want to use the code above, you'll need to import MediaPlayer) – Lukas1 Feb 10 '16 at 10:59
  • I'm still unable to reproduce it! The only thing I can suggest is that you create a new empty project, and start transferring code over to it until the memory starts to leak, that could help you identify exactly which bit is causing the leak. – Hamish Feb 10 '16 at 12:25
  • @originaluser2 thank you; That's what I started doing in the meantime... So far no luck.. I'll continue. thanks again for your suggestions. At least I know that I understood correctly memory management, that's important as well :-) – Lukas1 Feb 10 '16 at 12:28

0 Answers0