From 412e99b5b623bff6e50574fe4a6e2177bda1c355 Mon Sep 17 00:00:00 2001 From: Hugo GUERRIER Date: Wed, 12 Apr 2023 09:55:57 +0200 Subject: [PATCH] Correct the 'free' usage in the Java bindings to avoid errors on Windows --- langkit/templates/java_api/jni_impl_c.mako | 2 +- langkit/templates/java_api/main_class.mako | 6 +++--- langkit/templates/java_api/ni_lib.mako | 9 +++++++++ 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/langkit/templates/java_api/jni_impl_c.mako b/langkit/templates/java_api/jni_impl_c.mako index e32ce41de..f1cddf7b5 100644 --- a/langkit/templates/java_api/jni_impl_c.mako +++ b/langkit/templates/java_api/jni_impl_c.mako @@ -2161,7 +2161,7 @@ ${api.jni_func_sig("unit_filename", "jstring")}( jstring res = to_j_string(env, res_native); // Free the native allocated string - free((void *) res_native); + ${nat("free")}((void *) res_native); // Return the result return res; diff --git a/langkit/templates/java_api/main_class.mako b/langkit/templates/java_api/main_class.mako index db9273be5..6e56e36be 100644 --- a/langkit/templates/java_api/main_class.mako +++ b/langkit/templates/java_api/main_class.mako @@ -2632,11 +2632,11 @@ public class ${ctx.lib_name.camel} { PointerWrapper reference; if(ImageInfo.inImageCode()) { - CCharPointer charsetNative = + final CCharPointer charsetNative = charset == null ? WordFactory.nullPointer() : toCString(charset); - AnalysisContextNative resNative = + final AnalysisContextNative resNative = NI_LIB.${nat("allocate_analysis_context")}(); NI_LIB.${nat("initialize_analysis_context")}( @@ -3060,7 +3060,7 @@ public class ${ctx.lib_name.camel} { this.reference.ni() ); absoluteFile = toJString(resNative); - UnmanagedMemory.free(resNative); + NI_LIB.${nat("free")}(resNative); } else { absoluteFile = JNI_LIB.${nat("unit_filename")}(this); } diff --git a/langkit/templates/java_api/ni_lib.mako b/langkit/templates/java_api/ni_lib.mako index 4c48be93c..c95fb704b 100644 --- a/langkit/templates/java_api/ni_lib.mako +++ b/langkit/templates/java_api/ni_lib.mako @@ -309,6 +309,15 @@ ${exts.include_extension(ctx.ext("java_api", "ni_funcs"))} + // ----- Util functions ----- + + /** Util function to free langkit side allocated memory */ + @CompilerDirectives.TruffleBoundary + @CFunction + public static native void ${nat("free")}( + PointerBase pointer + ); + // ----- Exception functions ----- /** Get the last exception raised by langkit */