Skip to content

Commit

Permalink
work around hang when deleting a TempAllocatorMalloc on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Jul 8, 2024
1 parent 46522a1 commit 36a1647
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,25 @@ public TempAllocatorMalloc() {
setVirtualAddress(allocatorVa, true);
}
// *************************************************************************
// TempAllocator methods

/**
* Unassign the assigned native object, assuming there is one. Free the
* native object if the current allocator owns it.
*/
@Override
public void close() {
if (ownsNativeObject()) {
long allocatorVa = va();
free(allocatorVa);
}

unassignNativeObject();
}
// *************************************************************************
// private methods

native private static long create();

native private static void free(long allocatorVa);
}
15 changes: 15 additions & 0 deletions src/main/native/glue/TempAllocatorMalloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,18 @@ JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_TempAllocatorMalloc_
NonCopyable * const pResult = new TempAllocatorMalloc();
return reinterpret_cast<jlong> (pResult);
}

/*
* Class: com_github_stephengold_joltjni_TempAllocatorMalloc
* Method: free
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_TempAllocatorMalloc_free
(JNIEnv *, jclass, jlong allocatorVa) {
#ifndef WIN32
// Attempting to delete a TempAllocatorMalloc on Windows causes deadlock!
const NonCopyable * const pAllocator
= reinterpret_cast<NonCopyable *> (allocatorVa);
delete pAllocator;
#endif
}

0 comments on commit 36a1647

Please sign in to comment.