JsVar leak ?

Posted on
  • I've a memory leak (JsVars) in my bluetooth implementation.
    For easy testing I built a simple testfunction.
    After each call of this function, process.memory.free is reduced by 2
    Whats wrong in my code.

    /*JSON{
    	"type"	: "staticmethode",
    	"class"	: "ESP32",
    	"name"	: "test",
    	"generate" : "jswrap_ESP32_test",
    	"params":[ ["text","JsVar","test text"] ]
    }
    for testing only
    */
    JsVar *tmp;
    void jswrap_ESP32_test(JsVar *text){
    	char txt[20];
    	jsvGetString(text,txt,sizeof(txt)); // this is a short replacment of the data I get in Bluetooth
    	tmp = jsvNewStringOfLength(sizeof(txt), txt);
    	return;
    }
    

    Tried to add jsvUnLock and/or jsvUnRef on tmp, and sooner or later got HALT

  • You're not unlocking the string created by jsvNewStringOfLength...

    This should be fine?

    void jswrap_ESP32_test(JsVar *text){
        char txt[20];
        jsvGetString(text,txt,sizeof(txt)); // this is a short replacment of the data I get in Bluetooth
        JsVar *tmp = jsvNewStringOfLength(sizeof(txt), txt);
        jsvUnLock(tmp);
    }
    

    I would strongly advise not keeping a global JsVar* around though - because you have to be sure to get rid of it in a _kill handler for your module (and also initialise it to 0!). If you're planning on storing data, I'd do it as a named JsVar in execInfo.hiddenRoot and grab it whenever you want it instead.

  • Switched to execInfo.hiddenRoot and got everything running.
    This leak is history.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

JsVar leak ?

Posted by Avatar for JumJum @JumJum

Actions