Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

call C++ destructor instead of free #21

Merged
merged 2 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion java/com_spotify_voyager_jni_Index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,8 @@ void Java_com_spotify_voyager_jni_Index_nativeDestructor(JNIEnv *env,
jobject self) {
try {
if (Index *index = getHandle<Index>(env, self, true)) {
free(index);
delete index;
setHandle<Index>(env, self, nullptr);
}
} catch (std::exception const &e) {
if (!env->ExceptionCheck()) {
Expand Down
9 changes: 9 additions & 0 deletions java/src/main/java/com/spotify/voyager/jni/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,15 @@ public void close() throws IOException {
nativeDestructor();
}

@Override
protected void finalize() throws Throwable {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is called by the JVM on garbage collection so the destructor will be called even if close isn't called

try {
nativeDestructor();
} finally {
super.finalize();
}
}

private native void nativeConstructor(
SpaceType space,
int numDimensions,
Expand Down
Loading