Skip to content

Adjust WebBridge causes a memory leak #761

Open
@Chupik

Description

@Chupik

Hello!
We integrated Adjust WebBridge and after some tests noticed, that this class causes a memory leak.

What do we do?

  1. For example, we have WebPageView: UIView, that contains property let webView = WKWebView()
  2. Also, we added property let adjustBridge: AdjustBridge = AdjustBridge() in WebPageView
  3. Next, we overried init() method of WebPageView and added adjustBridge.loadWKWebViewBridge(webView)
  4. After this, we tested, that Adjust WebBridge JS interface works correctly and we can see our events using testing console.

What goes wrong?

When we close the instance of WebPageView, we can see that WKWebView is still present in memory.

What causes the memory leak

Fortunately, AdjustWebBridge is an open-source framework and we can see the implementation. So, during the AdjustBridge initialization, framework:

  1. Stores the strong reference to our WKWebView instance.
  2. Calls method addScriptMessageHandler and pass self (instance of AdjustBridge) as a parameter. Note, that addScriptMessageHandler adds the strong reference of AdjustBridge to our WKWebView
  3. After this, we have the retain cycle: AdjustBridge -> WKWebView -> (private property) scriptMessagehandlersArray -> AdjustBridge

How can you fix this

The most simple way to do this - make webView propery in AdjustBridge weak.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions