Skip to content

Commit

Permalink
* Make sure Generator ignores deallocators on const values retur…
Browse files Browse the repository at this point in the history
…ned from adapters (issue #317)
  • Loading branch information
saudet committed Jul 5, 2019
1 parent 16e0619 commit dfdf971
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

* Make sure `Generator` ignores deallocators on `const` values returned from adapters ([issue #317](https://github.com/bytedeco/javacpp/issues/317))
* Accelerate `Loader.extractResource()` for directories already cached, also preventing failures ([issue #197](https://github.com/bytedeco/javacpp/issues/197))
* Avoid `Parser` writing `allocateArray()` when single `int`, `long`, `float`, or `double` constructor already exists ([issue bytedeco/javacv#1224](https://github.com/bytedeco/javacv/issues/1224))
* Expose all platform properties to process executed with `Builder.buildCommand` via environment variables, with names uppercase and all `.` replaced with `_`
Expand Down
24 changes: 15 additions & 9 deletions src/main/java/org/bytedeco/javacpp/tools/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ static enum LongEnum { LONG; long value; }
final Properties properties;
final String encoding;
PrintWriter out, out2;
IndexedSet<String> callbacks;
Map<String,String> callbacks;
IndexedSet<Class> functions, deallocators, arrayDeallocators, jclasses;
Map<Class,Set<String>> members, virtualFunctions, virtualMembers;
Map<Method,MethodInformation> annotationCache;
Expand All @@ -186,7 +186,7 @@ public boolean generate(String sourceFilename, String headerFilename, String loa
@Override public void close() { }
});
out2 = null;
callbacks = new IndexedSet<String>();
callbacks = new LinkedHashMap<String,String>();
functions = new IndexedSet<Class>();
deallocators = new IndexedSet<Class>();
arrayDeallocators = new IndexedSet<Class>();
Expand Down Expand Up @@ -1319,7 +1319,7 @@ boolean classes(boolean handleExceptions, boolean defineAdapters, boolean conver
}
}
out.println();
for (String s : callbacks) {
for (String s : callbacks.values()) {
out.println(s);
}
out.println();
Expand All @@ -1328,10 +1328,12 @@ boolean classes(boolean handleExceptions, boolean defineAdapters, boolean conver
out.print("static void " + name + "_deallocate(void *p) { ");
if (FunctionPointer.class.isAssignableFrom(c)) {
String typeName = functionClassName(c);
out.println("\n int n = sizeof(" + typeName + "_instances) / sizeof(" + typeName + "_instances[0]);"
+ "\n for (int i = 0; i < n; i++) { if (" + typeName + "_instances[i].obj == (("
+ typeName + "*)p)->obj) " + typeName + "_instances[i].obj = NULL; }"
+ "\n JNIEnv *e; bool a = JavaCPP_getEnv(&e); if (e != NULL) e->DeleteWeakGlobalRef((jweak)(("
if (callbacks.containsKey(typeName)) {
out.print("\n int n = sizeof(" + typeName + "_instances) / sizeof(" + typeName + "_instances[0]);"
+ "\n for (int i = 0; i < n; i++) { if (" + typeName + "_instances[i].obj == (("
+ typeName + "*)p)->obj) " + typeName + "_instances[i].obj = NULL; }");
}
out.println("\n JNIEnv *e; bool a = JavaCPP_getEnv(&e); if (e != NULL) e->DeleteWeakGlobalRef((jweak)(("
+ typeName + "*)p)->obj); delete (" + typeName + "*)p; JavaCPP_detach(a); }");
} else if (virtualFunctions.containsKey(c)) {
String[] typeName = cppTypeName(c);
Expand Down Expand Up @@ -2437,7 +2439,11 @@ void returnAfter(MethodInformation methodInfo) {
if (Pointer.class.isAssignableFrom(methodInfo.returnType)) {
out.println(indent + "void* rowner = radapter.owner;");
}
out.println(indent + "void (*deallocator)(void*) = &" + adapterInfo.name + "::deallocate;");
if (typeName[0].startsWith("const ")) {
out.println(indent + "void (*deallocator)(void*) = 0;");
} else {
out.println(indent + "void (*deallocator)(void*) = &" + adapterInfo.name + "::deallocate;");
}
}
needInit = true;
} else if (returnBy instanceof ByVal ||
Expand Down Expand Up @@ -2696,7 +2702,7 @@ void callback(Class<?> cls, Method callbackMethod, String callbackName, int allo
}
memberList.add(member);
} else if (callbackName != null) {
callbacks.index("static " + instanceTypeName + " " + instanceTypeName + "_instances[" + allocatorMax + "];");
callbacks.put(instanceTypeName, "static " + instanceTypeName + " " + instanceTypeName + "_instances[" + allocatorMax + "];");
Convention convention = cls.getAnnotation(Convention.class);
if (convention != null && !convention.extern().equals("C")) {
out.println("extern \"" + convention.extern() + "\" {");
Expand Down

0 comments on commit dfdf971

Please sign in to comment.