Skip to content
New issue

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

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

Already on GitHub? # to your account

Memory Leak Issue Due to Retain Cycles in onButtonClicked, onActionButtonClicked, and onTextChanged #15

Open
NnStudio8 opened this issue Sep 7, 2023 · 0 comments

Comments

@NnStudio8
Copy link

NnStudio8 commented Sep 7, 2023

Issue Description:
I've noticed that the SwiftAlertView object is not getting deallocated properly due to strong reference cycles. Specifically, the onButtonClicked, onActionButtonClicked, and onTextChanged closure properties are causing a memory leak by retaining self strongly.

Suggested Fix:
To break the strong reference cycle, I've used [unowned self] in the closure. Here's how I've modified the code to fix the memory leak issue:

// handle events
@discardableResult
public func onButtonClicked(_ handler: @escaping (_ alertView: SwiftAlertView, _ buttonIndex: Int) -> Void) -> SwiftAlertView {
    self.onButtonClicked = { [unowned self] index in
        handler(self, index)
    }
    return self
}

@discardableResult
public func onActionButtonClicked(_ handler: @escaping (_ alertView: SwiftAlertView, _ buttonIndex: Int) -> Void) -> SwiftAlertView {
    self.onActionButtonClicked = { [unowned self] index in
        handler(self, index)
    }
    return self
}

@discardableResult
public func onTextChanged(_ handler: @escaping (_ alertView: SwiftAlertView, _ text: String?, _ textFieldIndex: Int) -> Void) -> SwiftAlertView {
    self.onTextChanged = { [unowned self] text, index in
        handler(self, text, index)
    }
    return self
}

I hope this helps to resolve the issue. Looking forward to your feedback.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant