mongoose-os-libs / wifi

Other
13 stars 29 forks source link

mgos_wifi_get_connected_ssid() causes memory leak if called in mJS #25

Open tripflex opened 5 years ago

tripflex commented 5 years ago

As mentioned in Gitter chat, there's no easy way in mJS to get the connected SSID or connection status, without a memory leak, or extra mJS code to get the pointer, then the value, then free afterwards.

As i'm sure others will need this in mJS -- there should be an easy way to get these values without having the memory leak or using a hack in mJS to get the value then free it.


Provided by @cpq on Gitter one way of doing it could be something like that - FFI as a void, not as char

let func = ffi('void *foo()');
let strlen = ffi('int strlen(char *)');
let ptr = f();
let str = mkstr(ptr, strlen(ptr));
Sys.free(ptr);

so the idea is that by FFI-ing void *, you get the pointer

UPDATE: The above code DOES NOT WORK CORRECTLY -- I had to add my own strlen with a void * arg to make this work:

int smyles_strlen(void *something){
    return strlen( (const char*)something );
}

Then use:

let smyL = ffi('int smyles_strlen(void *)');
let ssidF = ffi("void *mgos_wifi_get_connected_ssid()");
let ssidP = ssidF();
let ssidS = mkstr(ssidP, smyL(ssidP));
... do something with ssidS
Sys.free(ssidP);

IT SHOULDN'T BE THIS DIFFICULT TO GET SSID FROM mJS!!