0

Which of the following scenarios is correct coding practice, given that player is (nonatomic, retain), and is synthesized using player = _player.

Scenario A

MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
        self.player = mp;
        [mp release];

Scenario B

MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
    self.player = mp;

Up until this point I have been using scenario A as a general practice, but I think this may be causing memory leaks within my code.

Thanks for any help.

EDIT 1:

And does the same apply to Timers, they are causing me real hassle. If I'm using the code below is this correct? If timerMap is also (nonatomic, retain), and uses timerMap = _timerMap;

self.timerMap = [[NSTimer scheduledTimerWithTimeInterval:fps target:self selector:@selector(updateAnimationTimer) userInfo:nil repeats:YES] autorelease];

And when releasing is it fine to just invalidate, or should it be invalidate then release?

Elliott D'Alvarez
  • 1,217
  • 16
  • 30
  • See [this answer](http://stackoverflow.com/questions/8576593/objective-c-memory-management-of-instance-members/8576760#8576760) for explanation of what is going on with synthesize and properties – Nick Bull Jul 23 '12 at 10:20

3 Answers3

4

Scenario A is definitely the way to go, Scenario B will leak mp

Source : Apple Doc

EDIT 1 :

The code you posted is wrong, it will result in a crash when setting the timerMap property again. You must not autorelease it

self.timerMap = [NSTimer scheduledTimerWithTimeInterval:fps target:self selector:@selector(updateAnimationTimer) userInfo:nil repeats:YES];

When you don't need the timer anymore, just

self.timerMap = nil; 

This will call the release method on the timer and will set the pointer to nil

Olotiar
  • 3,225
  • 1
  • 18
  • 37
  • Great thanks, my application must have a memory leak from elsewhere then. Just annoying it's not being picked up by instruments :( – Elliott D'Alvarez Jul 23 '12 at 10:18
  • The Project -> Analyse tool is good for picking up memory leaks (it would have picked up scenario B). Have a try ! – Olotiar Jul 23 '12 at 10:20
1

Scenario A is correct. Scenario B actually will cause memory leak, it's because self.player = mp; retains the reference counter.

Following code is also correct:

MPMoviePlayerController *mp = [[[MPMoviePlayerController alloc] initWithContentURL:movieURL] autorelease];
self.player = mp;
Ilya K.
  • 940
  • 6
  • 16
1

The first one is correct memory management, however I just tend to go for:

self.player = [[[MPMoviePlayerController alloc] 
                 initWithContentURL:movieURL] 
                 autorelease];

This way I keep all the memory management for that object on one line

EDIT following edit to question

The timer object as you have it there is already autorelease, you shouldn't add autorelease to it again. Have a look at this question for an explanation of convenience methods.

Community
  • 1
  • 1
James Webster
  • 31,873
  • 11
  • 70
  • 114