Understanding memory management in native extension (HTML platform)

In which case is it necessary to clear memory from the C side, and in which case is it not?

Do I understand correctly that:

Sample 1:

    JsExt_SomeFunction: function () {
        let value = "value";
        return stringToUTF8OnStack(value);
    },
static int SomeFunction(lua_State* L)
{
    DM_LUA_STACK_CHECK(L, 1);
    const char* value = JsExt_SomeFunction();
    lua_pushstring(L, value);
//    free((void*)value);  no cleaning required???
    return 1;
}

Sample 2:

    JsExt_SomeFunction: function () {
        let value = "value";
        return stringToUTF8(value);
    },
static int SomeFunction(lua_State* L)
{
    DM_LUA_STACK_CHECK(L, 1);
    const char* value = JsExt_SomeFunction();
    lua_pushstring(L, value);
    free((void*)value);  //cleaning required???
    return 1;
}

Where can I read more about all these things, plus about makeDynCall?

I couldn’t find any documentation on that function.
Only stringToUTF8:
https://emscripten.org/docs/api_reference/preamble.js.html?highlight=stringtoutf8#stringToUTF8
The heap allocated strings should be freed.

However, as your function name suggests, it’s allocated on the stack, so if you don’t store the pointer, you shouldn’t have to free it on the C side.

Where can I read more about all these things, plus about makeDynCall?

For Emscripten documentation, we refer to their page:
https://emscripten.org/docs/api_reference/index.html

1 Like