Events API should make clear the memory management strategy

Events API:
https://docs.flightsimulator.com/msfs2024/html/6_Programming_APIs/WASM/Event_API/Event_API.htm

Samples on this page use both malloc and new, and never use free or delete. ~I’m pretty sure~ The memory needs to be cleaned up after the call, otherwise sending a key event will leak a few bytes every time. ~Alternatively, do these APIs take ownership of that memory?~

#define MY_KEY_EVENT
...
FsVarParamArray paramToSend;
paramToSend.size = 2;
paramToSend.array = (FsVarParamVariant*) malloc(2 * sizeof(FsVarParamVariant));
// Init paramToSend
fsEventsTriggerKeyEvent(MY_KEY_EVENT, paramToSend)

This lifecycle looks like after unregister the memory needs to be reclaimed.

void EventHandler(FsEventId id, FsVarParamArray* params, void* userParams)
{
    ...
}
DATA* userData = new DATA();
fsRegisterKeyEventHandler(EventHandler, userData);
...
fsUnregisterKeyEventHandler(EventHandler, userData);

EDIT 1: Found /// /!\ DO NOT FORGET TO FREE PARAM ARRAY /!\ in the SDK source.

FsCreateParamArray and FsDestroyParamArray uses free.

So in the end it is standard memory management. The docs are missing free calls but devs are still responsible to free that memory as normal.

2 Likes