mirror of
https://github.com/kiwix/java-libkiwix.git
synced 2025-09-09 23:30:34 -04:00
Fixed the native crash happening on Android.
* Improved the `getPtr` method to throw a `NativeHandleDisposedException` when a native object is `nullptr`, updated the `CATCH_EXCEPTION` macro to handle this exception by mapping it to `java.lang.IllegalStateException`, and ensured proper error handling to prevent crashes when accessing disposed objects.
This commit is contained in:
parent
4c6dadba84
commit
c5de943905
@ -53,6 +53,9 @@ catch(const zim::ZimFileFormatError& e) { \
|
|||||||
} catch(const zim::EntryNotFound& e) { \
|
} catch(const zim::EntryNotFound& e) { \
|
||||||
throwException(env, "org/kiwix/libzim/EntryNotFoundException", e.what()); \
|
throwException(env, "org/kiwix/libzim/EntryNotFoundException", e.what()); \
|
||||||
return RET; \
|
return RET; \
|
||||||
|
} catch (const NativeHandleDisposedException& e) { \
|
||||||
|
throwException(env, "java/lang/IllegalStateException", e.what()); \
|
||||||
|
return RET; \
|
||||||
} catch (const std::ios_base::failure& e) { \
|
} catch (const std::ios_base::failure& e) { \
|
||||||
throwException(env, "java/io/IOException", e.what()); \
|
throwException(env, "java/io/IOException", e.what()); \
|
||||||
return RET; \
|
return RET; \
|
||||||
|
@ -96,6 +96,11 @@ inline void setHandle(JNIEnv* env, jobject thisObj, Args && ...args)
|
|||||||
}
|
}
|
||||||
#define SET_HANDLE(NATIVE_TYPE, OBJ, VALUE) setHandle<NATIVE_TYPE>(env, OBJ, VALUE)
|
#define SET_HANDLE(NATIVE_TYPE, OBJ, VALUE) setHandle<NATIVE_TYPE>(env, OBJ, VALUE)
|
||||||
|
|
||||||
|
class NativeHandleDisposedException : public std::runtime_error {
|
||||||
|
public:
|
||||||
|
explicit NativeHandleDisposedException(const std::string& message)
|
||||||
|
: std::runtime_error(message) {}
|
||||||
|
};
|
||||||
|
|
||||||
// Return a shared_ptr for the handle
|
// Return a shared_ptr for the handle
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@ -104,6 +109,9 @@ shared_ptr<T> getPtr(JNIEnv* env, jobject thisObj, const char* handleName = "nat
|
|||||||
jclass thisClass = env->GetObjectClass(thisObj);
|
jclass thisClass = env->GetObjectClass(thisObj);
|
||||||
jfieldID fidNumber = env->GetFieldID(thisClass, handleName, "J");
|
jfieldID fidNumber = env->GetFieldID(thisClass, handleName, "J");
|
||||||
auto handle = reinterpret_cast<shared_ptr<T>*>(env->GetLongField(thisObj, fidNumber));
|
auto handle = reinterpret_cast<shared_ptr<T>*>(env->GetLongField(thisObj, fidNumber));
|
||||||
|
if (handle == nullptr) {
|
||||||
|
throw NativeHandleDisposedException("The native object is already has been disposed");
|
||||||
|
}
|
||||||
return *handle;
|
return *handle;
|
||||||
}
|
}
|
||||||
#define GET_PTR(NATIVE_TYPE) getPtr<NATIVE_TYPE>(env, thisObj)
|
#define GET_PTR(NATIVE_TYPE) getPtr<NATIVE_TYPE>(env, thisObj)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user