Skip to content

Commit

Permalink
* Fix some more UniquePtrAdapter incorrectly deallocating pointers…
Browse files Browse the repository at this point in the history
… on callbacks (issue #613)
  • Loading branch information
saudet committed Oct 26, 2022
1 parent 81252dd commit 45a6a4b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
7 changes: 6 additions & 1 deletion src/main/java/org/bytedeco/javacpp/tools/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -1388,6 +1388,7 @@ boolean classes(boolean handleExceptions, boolean defineAdapters, boolean conver
out.println(" UniquePtrAdapter( U& uniquePtr) : ptr(0), size(0), owner(0), uniquePtr2(U(NULL, D())), uniquePtr(uniquePtr) { }");
out.println(" UniquePtrAdapter(const U* uniquePtr) : ptr(0), size(0), owner(0), uniquePtr2(U(NULL, D())), uniquePtr(*(U*)uniquePtr) { }");
out.println(" void assign(T* ptr, size_t size, void* owner) {");
out.println(" if (this->ptr == ptr && this->size == size && this->owner == owner) return;");
out.println(" this->ptr = ptr;");
out.println(" this->size = size;");
out.println(" this->owner = owner;");
Expand Down Expand Up @@ -3245,7 +3246,11 @@ void callback(Class<?> cls, Method callbackMethod, String callbackName, int allo

if (adapterInfo != null) {
usesAdapters = true;
out.println(" " + adapterInfo.name + " adapter" + j + "(arg" + j + ");");
String cast2 = adapterInfo.cast2.trim();
if (cast2.length() > 0 && !cast2.startsWith("(") && !cast2.endsWith(")")) {
cast2 = "(" + cast2 + ")";
}
out.println(" " + adapterInfo.name + " adapter" + j + "(" + cast2 + "arg" + j + ");");
}

if (Pointer.class.isAssignableFrom(callbackParameterTypes[j]) ||
Expand Down
22 changes: 14 additions & 8 deletions src/test/java/org/bytedeco/javacpp/AdapterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,16 +271,19 @@ static class UniqueFunction extends FunctionPointer {
sharedData = fetchSharedData();
assertEquals(13, sharedData.data());

final SharedData[] sharedData2 = new SharedData[1];
int data = testCallback(new SharedFunction() {
@Override public int call(SharedData d) {
int data = d.data();
d.deallocate();
return 2 * data;
d.data(2 * d.data());
sharedData2[0] = d;
return 3 * d.data();
}
}, sharedData);
assertEquals(2 * 13, data);
assertEquals(2 * 13, sharedData2[0].data());
assertEquals(2 * 3 * 13, data);

sharedData.deallocate();
sharedData2[0].deallocate();

assertEquals(1, constructorCount());
assertEquals(1, destructorCount());
Expand Down Expand Up @@ -310,14 +313,17 @@ static class UniqueFunction extends FunctionPointer {
uniqueData = fetchUniqueData();
assertEquals(42, uniqueData.data());

final UniqueData[] uniqueData2 = new UniqueData[1];
int data = testCallback(new UniqueFunction() {
@Override public int call(UniqueData d) {
int data = d.data();
d.deallocate();
return 2 * data;
d.data(2 * d.data());
uniqueData2[0] = d;
return 3 * d.data();
}
}, uniqueData);
assertEquals(2 * 42, data);
assertEquals(2 * 42, uniqueData2[0].data());
assertEquals(2 * 3 * 42, data);
uniqueData2[0].deallocate();
System.gc();
}

Expand Down

0 comments on commit 45a6a4b

Please sign in to comment.