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

FABMenu: memory issue (reference cycle) #820

Closed
comaxime opened this issue Aug 1, 2017 · 3 comments
Closed

FABMenu: memory issue (reference cycle) #820

comaxime opened this issue Aug 1, 2017 · 3 comments
Assignees
Labels

Comments

@comaxime
Copy link

comaxime commented Aug 1, 2017

Hello,
I have found a problem of reference cycle between FABMenu and FABMenuController.

FABMenuController has a reference to FABMenu:

open class FABMenuController: RootController {
    open let fabMenu = FABMenu()
}

FABMenu has references to FABMenuController via the 4 callbacks:

extension FABMenuController {
    fileprivate func prepareFABMenu() {
        ...
        fabMenu.handleFABButtonCallback = handleFABButtonCallback
        fabMenu.handleOpenCallback = handleOpenCallback
        fabMenu.handleCloseCallback = handleCloseCallback
        fabMenu.handleCompletionCallback = handleCompletionCallback
    }
}

One way to avoid this is to use weak references in the callbacks:

fabMenu.handleFABButtonCallback = { [weak self] button in self?.handleFABButtonCallback(button: button) }
fabMenu.handleOpenCallback = { [weak self] in self?.handleOpenCallback() }
fabMenu.handleCloseCallback = { [weak self] in self?.handleCloseCallback() }
fabMenu.handleCompletionCallback = { [weak self] view in self?.handleCompletionCallback(view: view) }

This did fix the problem in my case.

Am I correct or am I missing something?
Do you see better ways to solve this problem?

Thanks.

@daniel-jonathan daniel-jonathan self-assigned this Aug 1, 2017
@daniel-jonathan
Copy link
Member

I will take a look, nice catch :)

@comaxime
Copy link
Author

comaxime commented Aug 2, 2017

thanks @DanielDahan ;)

@daniel-jonathan
Copy link
Member

@comaxime For now, your solution looks great :) I am going to make a push now. All the best!

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

No branches or pull requests

2 participants