DirectMyFile / syscall

System Calls for Dart
Other
11 stars 2 forks source link

If the binary callback (as and any other binary data) used not immediately it is required to keep it alive all time that it used #6

Open mezoni opened 9 years ago

mezoni commented 9 years ago
static void bindKey(key, Function handler) {
    LibReadline.init();

    var functionType = getBinaryType("rl_command_func_t");

    var callback = new BinaryCallback(functionType, (args) {
      if (handler is ReadlineCommandRegularFunction) {
        return handler(args[0], args[1]);
      } else {
        return handler();
      }
    });

    checkSysCallResult(invoke("readline::rl_bind_key", [key is String ? key.codeUnitAt(0) : key, callback.functionCode]));
  }

After the last use (in checkSysCallResult) of the binary data (BinaryCallback) it will be freed by the garbage collector at the first opportunity. Binary function code also would be invalid (deallocated).

I do not know how the "readline" works but if you want create a "long life" binary callback you should return it back and store it in a safe place.

Local variables not a good place. If it used only in the function body then you can use method keepAlive()

var data = allocData();
someFunc(data);
// above we not use data directly
// but some code may use data in physical memory
// Without this data would be freed
// Keep it alive
keepAlive(data);
azenla commented 9 years ago

Ok. I'll do that.