Skip to content

Commit

Permalink
Merge branch 'topic/free_usage_in_java' into 'master'
Browse files Browse the repository at this point in the history
Correct the 'free' usage in the Java bindings to avoid errors on Windows

Closes AdaCore#648

See merge request eng/libadalang/langkit!832
  • Loading branch information
HugoGGuerrier committed Apr 17, 2023
2 parents ccf8711 + 412e99b commit 85d71d7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion langkit/templates/java_api/jni_impl_c.mako
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions langkit/templates/java_api/main_class.mako
Original file line number Diff line number Diff line change
Expand Up @@ -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")}(
Expand Down Expand Up @@ -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);
}
Expand Down
9 changes: 9 additions & 0 deletions langkit/templates/java_api/ni_lib.mako
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit 85d71d7

Please sign in to comment.