From c9f30a361ca9bd9d5ac1e2ddfeebbf2a076fedcd Mon Sep 17 00:00:00 2001 From: Joshua Suskalo Date: Sat, 16 Jul 2022 19:26:27 -0500 Subject: [PATCH] Add segment in readme about anonymous functions as callbacks --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 461233e..d574274 100644 --- a/README.md +++ b/README.md @@ -239,6 +239,13 @@ type and message in the registers section, but it's important to be aware of all the same. Ideally you should test your callbacks before actually passing them to native code. +Another important thing to keep in mind is the expected lifetime of the function +that you pass to native code. For example it is perfectly fine to pass an +anonymous function to a native function if the callback will never be called +again once the native function returns. If however it saves the callback for +later use the JVM may collect it prematurely, causing a crash when the callback +is later called by native code. + ### Variadic Functions Some native functions can take any number of arguments, and in these cases coffi provides `vacfn-factory` (for "varargs C function factory").