Skip to content

Commit 121b012

Browse files
committed
fix: Initialize VIPS_INIT only once
Mihon was crashing somethimes crashing when decoding the first image. I notice Mihon was decoding two images at the same time, which made me think it could be a threading issue. Reading libvips documentation, it states that it is safe, but VIPS_INIT must only be called in a single-thread. I was calling VIPS_INIT for every decoder created, which I believe was the issue. This commit moves VIPS_INIT to JNI_OnLoad, which is called only once when the library is loaded.
1 parent 94ad0de commit 121b012

2 files changed

Lines changed: 6 additions & 6 deletions

File tree

library/src/main/cpp/image-decoder/decoder_vips.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,6 @@ VipsDecoder* try_vips_decoder(std::shared_ptr<Stream>& stream, bool cropBorders,
2626
VipsDecoder::VipsDecoder(std::shared_ptr<Stream>&& stream, bool cropBorders,
2727
cmsHPROFILE targetProfile)
2828
: stream(std::move(stream)), targetProfile(targetProfile) {
29-
30-
if (VIPS_INIT("VipsDecoder")) {
31-
LOGE("Failed to initialize libvips.");
32-
throw std::runtime_error("libvips initialization failed");
33-
}
34-
3529
// the VImage does not take ownership of the `stream`.
3630
this->image = VImage::new_from_buffer(
3731
this->stream->bytes, this->stream->size, nullptr,

library/src/main/cpp/image-decoder/java_wrapper.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ jint JNI_OnLoad(JavaVM* vm, void*) {
2121
} else {
2222
return JNI_ERR;
2323
}
24+
25+
if (VIPS_INIT("VipsDecoder")) {
26+
LOGE("Failed to initialize libvips.");
27+
return JNI_ERR;
28+
}
29+
2430
return JNI_VERSION_1_6;
2531
}
2632

0 commit comments

Comments
 (0)