diff --git a/modules/juce_core/native/juce_Files_android.cpp b/modules/juce_core/native/juce_Files_android.cpp index 5e7885ce74..70bec895fa 100644 --- a/modules/juce_core/native/juce_Files_android.cpp +++ b/modules/juce_core/native/juce_Files_android.cpp @@ -560,8 +560,8 @@ struct AndroidStreamHelpers class AndroidInputStreamWrapper final : public InputStream { public: - explicit AndroidInputStreamWrapper (jobject streamIn) - : stream (LocalRef { streamIn }) + explicit AndroidInputStreamWrapper (LocalRef streamIn) + : stream (std::move (streamIn)) { } @@ -577,7 +577,7 @@ public: AndroidInputStreamWrapper& operator= (AndroidInputStreamWrapper&& other) noexcept { - std::swap (*this, other); + AndroidInputStreamWrapper { std::move (other) }.swap (*this); return *this; } @@ -585,6 +585,9 @@ public: ~AndroidInputStreamWrapper() override { + if (stream == nullptr) + return; + getEnv()->CallVoidMethod (stream.get(), AndroidInputStream.close); jniCheckHasExceptionOccurredAndClear(); } @@ -647,14 +650,22 @@ private: return skipped == num; } + void swap (AndroidInputStreamWrapper& other) noexcept + { + std::swap (other.byteArray, byteArray); + std::swap (other.stream, stream); + std::swap (other.pos, pos); + std::swap (other.exhausted, exhausted); + } + CachedByteArray byteArray; GlobalRef stream; int64 pos = 0; bool exhausted = false; }; -std::unique_ptr makeAndroidInputStreamWrapper (jobject stream); -std::unique_ptr makeAndroidInputStreamWrapper (jobject stream) +std::unique_ptr makeAndroidInputStreamWrapper (LocalRef stream); +std::unique_ptr makeAndroidInputStreamWrapper (LocalRef stream) { return std::make_unique (stream); } @@ -672,7 +683,7 @@ struct AndroidContentUriInputStream final : public InputStream AndroidContentUriInputStream& operator= (AndroidContentUriInputStream&& other) noexcept { - std::swap (*this, other); + AndroidContentUriInputStream { std::move (other) }.swap (*this); return *this; } @@ -744,6 +755,12 @@ private: return getPosition() == oldPosition + num; } + void swap (AndroidContentUriInputStream& other) noexcept + { + std::swap (other.stream, stream); + std::swap (other.uri, uri); + } + AndroidInputStreamWrapper stream; GlobalRef uri; }; diff --git a/modules/juce_graphics/native/juce_Fonts_android.cpp b/modules/juce_graphics/native/juce_Fonts_android.cpp index 64dac9f2de..013d6a8248 100644 --- a/modules/juce_graphics/native/juce_Fonts_android.cpp +++ b/modules/juce_graphics/native/juce_Fonts_android.cpp @@ -93,7 +93,7 @@ DECLARE_JNI_CLASS (AndroidAssetManager, "android/content/res/AssetManager") #undef JNI_CLASS_MEMBERS // Defined in juce_core -std::unique_ptr makeAndroidInputStreamWrapper (jobject stream); +std::unique_ptr makeAndroidInputStreamWrapper (LocalRef stream); struct AndroidCachedTypeface {