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);
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()