Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak because of timer? #24

Closed
funkenstrahlen opened this issue Oct 24, 2015 · 3 comments
Closed

Memory leak because of timer? #24

funkenstrahlen opened this issue Oct 24, 2015 · 3 comments

Comments

@funkenstrahlen
Copy link
Contributor

I just discovered a problem: If you use a timer in the PopupViewController to call a method on it regularly the PlayerViewController will be kept in memory and active even if the tabBarController instantiated a new PopupViewController.

I discovered this because right at the moment I added a timer, my AVPlayer of each PopupViewController(containing the player) kept playing in the background even when I started playing another song and a new PopupViewController was created.

I fixed this by invalidating the timer each time a new PopupViewController object was created.

        // invalidate the timer of the old controller, because this keeps the old controller in the memory
        if let oldController = popupContentController {
            oldController.timer?.invalidate()
            oldController.player?.pause()
        }
        popupContentController = storyboard?.instantiateViewControllerWithIdentifier("DemoMusicPlayerController") as? PlayerViewController

        tabBarController?.presentPopupBarWithContentViewController(popupContentController!, animated: true, completion: nil)

Is this issue covered in the demo projects?

@funkenstrahlen
Copy link
Contributor Author

It can also be fixed by only keeping one single instance of the PopupViewController and not creating a new one each time.

@LeoNatan
Copy link
Owner

I don't understand the problem here. NSTimer retains the provided target until it finishes or is invalidated and released. It is clearly mentioned in the documentation:

The timer maintains a strong reference to target until it (the timer) is invalidated.

LNPopupController is not responsible for your app's logic and/or memory management. If you choose to use NSTimer, you must be aware of how it works, and prepare accordingly. There are several ways to handle memory management; one you have mentioned, another is to use a weak wrapper object as the target of the timer, and only perform the selector on the wrapped object if it is not nil.

I am closing this issue as it is not related to the framework in any way.

@funkenstrahlen
Copy link
Contributor Author

Yes you are right. It is not a problem of the framework itself. However I think it might be nice if the demo projects in this repository would take care of this problem. Maybe people using the demo projects as a template will not run into this problem then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants