Skip to content

Use WeakRefs in event handlers? #9820

Open

Description

@kripken

See e.g. #5456 (comment) and other reports in the past. If one creates event handlers then the module cannot be GC'd even if nothing but the event handlers refers to it. The handlers must be unregistered manually.

If all event handlers used a WeakRef to everything else, then the module can be gc'd, and then the event handler can see it can just exit (and unregister itself).

As WeakRefs are not supported everywhere yet, we'd need to check for their presence.

I'm not sure if this would need to be an option or not. Doing it automatically would help some things, but maybe some codebases do want to stay alive due to event handlers.

Activity

  1. juj commented on Nov 25, 2019

    @juj
    Collaborator

    If one does not want to manually track and release all event handlers, one can call the JS function removeAllEventListeners to do that in one go. That function should probably be hoisted to a extern void emscripten_remove_all_event_callbacks(void); in system/include/emscripten/html5.h to make it publicly accessible from C code, if someone wants to take a stab at that.

  2. stale commented on Nov 24, 2020

    @stale

    This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 30 days. Feel free to re-open at any time if this issue is still relevant.

  3. jeffRTC commented on May 7, 2021

    @jeffRTC

    @juj That doesn't works when you have a Dispatcher function where you create a new Module for each time for each event.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions