Skip to content

SDL_Init sets up event handlers which SQL_Quit does not release, which prevents GC of the Module instance  #6172

Closed

Description

@dmitrykuzmin

In my application, I compile emscripten code via "MODULARIZE" option and thus I am able to create multiple Module instances one after another.

But when it's time for the Module instance to be garbage collected and release its memory, it doesn't do so. Instead, it keeps the memory occupied.

I've created a small example showcasing the problem.

From what I see, the problem is in the following line:

SDL_Init(SDL_INIT_VIDEO);

This line is situated in the class method exposed to the JavaScript and, although the method is never called, it somehow prevents Module instance from being destroyed and releasing its memory. When the line is commented out, everything is garbage collected properly and memory is released.

Is there any solution or workaround for this problem?

Activity

  1. kripken commented on Feb 1, 2018

    @kripken
    Member

    Thanks for the testcase (I had to lower the amount of memory to 128MB or my browser wouldn't load it, but otherwise it all works as described).

    I believe what's going on is that SDL_Init sets up some event listeners, and they keep everything else alive.

    In principle calling SDL_Quit should remove those. However, it looks like we don't do that, which is a bug. I'll rename this issue to focus on that.

  2. changed the title [-]Unable to release memory from the Module instance[/-] [+]SDL_Init sets up event handlers which SQL_Quit does not release, which prevents GC of the Module instance [/+] on Feb 1, 2018
  3. juj commented on Feb 1, 2018

    @juj
    Collaborator

    Try checking browser's DOM Inspector tool to see which event handlers are still active, and see where they are registered, to get a possible clue where they should get unregistered in turn.

  4. dmitrykuzmin commented on Feb 2, 2018

    @dmitrykuzmin
    Author

    Update: I've found some more SDL commands that prevent Module instance from being garbage collected in the same way as SDL_Init does.

    They are as follows: SDL_CreateWindow, SDL_PollEvent and even SDL_Quit itself.
    Maybe there are more, but those are the ones I use in my program.

    I updated my test case with branches for each failing command:
    SDL_CreateWindow, SDL_PollEvent, SDL_Quit.

    For me they all work in exactly the same way as with SDL_Init.

    All these commands though create the same event listeners: gamepadconnected and gamepaddisconnected. When I delete those listeners manually from DevTools, memory is released correctly, so your theory seems to be true.

  5. added a commit that references this issue on Feb 6, 2018
    295c590
  6. stale commented on Sep 19, 2019

    @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 7 days. Feel free to re-open at any time if this issue is still relevant.

  7. ongamex commented on Oct 24, 2019

    @ongamex

    Well something similar looking, but that thing is now an error:

    Calling stub instead of sigaction() sge_game.html:1246:19
    printErr http://localhost:8080/sge_game.html:1246
    _sigaction http://localhost:8080/sge_game.js:12084
    _SDL_QuitInit http://localhost:8080/sge_game.js:308720
    _SDL_EventsInit http://localhost:8080/sge_game.js:305899
    _SDL_InitSubSystem http://localhost:8080/sge_game.js:300381
    _SDL_Init http://localhost:8080/sge_game.js:300575
    _main http://localhost:8080/sge_game.js:86826
    _main http://localhost:8080/sge_game.js:513764
    callMain http://localhost:8080/sge_game.js:514116
    doRun http://localhost:8080/sge_game.js:514175
    run http://localhost:8080/sge_game.js:514186

  8. sbc100 commented on Oct 25, 2019

    @sbc100
    Collaborator

    Are you building with USE_SDL=2? It looks like it because SDL_InitSubSystem does nothing in emscirpten normally. Might be worth filing a bug at https://github.com/emscripten-ports/SDL2? In any case this doesn't look related to this bug.

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