paullouisageneau / datachannel-wasm

C++ WebRTC Data Channels and WebSockets for WebAssembly in browsers
MIT License
148 stars 25 forks source link

RuntimeError: Aborted(free() called but not included in the build - add '_free' to EXPORTED_FUNCTIONS) #38

Closed ghost closed 2 years ago

ghost commented 2 years ago

So I managed to get dynCall by linking with -sEXPORTED_RUNTIME_METHODS=["dynCall"] -sDYNCALLS=1, but now I'm getting this error about not having free(). I've tried doing -sEXPORTED_RUNTIME_METHODS=["dynCall","_free"] when I link but It doesn't appear to do anything

Specifically i'm looking at the free() calls from handleCandidate and handeDescription

ghost commented 2 years ago

Oh, this only seems to be a problem with newer versions of emscripten. Trying out an old version of emscripten like 2.0.3, there's no need for -sDYNCALLS=1, and there's no complications in calling free()

ghost commented 2 years ago

According to their changelog at https://emscripten.org/docs/introducing_emscripten/release_notes.html#changelog:

2.0.4: 09/16/2020

ghost commented 2 years ago

huh, actually looks like linking works up to emscripten 2.0.9

paullouisageneau commented 2 years ago

There should be no need for DYNCALLS=1 as it enables a legacy API with functions like dynCall_vi, it is not necessary for the dynCall function:

2.0.13: 01/19/2021
------------------
 - Add back support for calling the legacy dynCall_sig() API to invoke function
  pointers to wasm functions from JavaScript. Pass -s DYNCALLS=1
  to include that functionality in the build. This fixes a regression that
  started in Aug 31st 2020 (Emscripten 2.0.2)

There is a confusion between EXPORTED_FUNCTIONS and EXPORTED_RUNTIME_METHODS: EXPORTED_FUNCTIONS makes functions accessible from the compiled C/C++ code (otherwise they might be removed to make the build smaller), and EXPORTED_RUNTIME_METHODS exposes runtime functions to JavaScript code.

Here _free ends up optimized out so you need to add it to EXPORTED_FUNCTIONS: -s EXPORTED_FUNCTIONS=["_free"]

ghost commented 2 years ago

-s EXPORTED_FUNCTIONS=["_free"] seems to have done the trick, thanks!

I still will get an ReferenceError: dynCall is not defined if I try to compile it without the -sDYNCALLS=1 flag

paullouisageneau commented 2 years ago

The dynCall issue should be fixed by https://github.com/paullouisageneau/datachannel-wasm/pull/39