shaka-project / shaka-player-embedded

Shaka Player in a C++ Framework
Apache License 2.0
236 stars 63 forks source link

Memory leak due to strong reference cycle in ShakaPlayerView #125

Closed jgongo closed 4 years ago

jgongo commented 4 years ago

I can't get the player to be properly destroyed, and I think I have found a reference cycle in ShakaPlayerView which prevents it to be destroyed (causing the rest of the object graph to be kept in memory).

ShakaPlayerView uses two timers:

  NSTimer *_renderLoopTimer;
  NSTimer *_textLoopTimer;

which are initialized in the initWithPlayer: initializer:

    SEL renderLoopSelector = @selector(renderLoop:);
    _renderLoopTimer = [NSTimer scheduledTimerWithTimeInterval:ShakaRenderLoopDelay
                                                        target:self
                                                      selector:renderLoopSelector
                                                      userInfo:nullptr
                                                       repeats:YES];
    _textLoopTimer = [NSTimer scheduledTimerWithTimeInterval:0.25
                                                      target:self
                                                    selector:@selector(textLoop:)
                                                    userInfo:nullptr
                                                     repeats:YES];

According to the documentation regarding the target parameter of scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: (bold highlight is mine):

The object to which to send the message specified by aSelector when the timer fires. The timer maintains a strong reference to target until it (the timer) is invalidated.

I haven't been able to find any place in the code where these timers are invalidated or destroyed (setting the reference to nil), so it seems there's a reference cycle preventing the ShakaPlayerView (and in turn, the associated ShakaPlayer) to be destroyed.

Am I right?