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

Fixed the native crash happening on Android. #116

Merged
merged 2 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 3 additions & 0 deletions lib/src/main/cpp/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ catch(const zim::ZimFileFormatError& e) { \
} catch(const zim::EntryNotFound& e) { \
throwException(env, "org/kiwix/libzim/EntryNotFoundException", e.what()); \
return RET; \
} catch (const NativeHandleDisposedException& e) { \
throwException(env, "java/lang/IllegalStateException", e.what()); \
return RET; \
} catch (const std::ios_base::failure& e) { \
throwException(env, "java/io/IOException", e.what()); \
return RET; \
Expand Down
8 changes: 8 additions & 0 deletions lib/src/main/cpp/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)

class NativeHandleDisposedException : public std::runtime_error {
public:
explicit NativeHandleDisposedException(const std::string& message)
: std::runtime_error(message) {}
};

// Return a shared_ptr for the handle
template<typename T>
Expand All @@ -104,6 +109,9 @@ shared_ptr<T> getPtr(JNIEnv* env, jobject thisObj, const char* handleName = "nat
jclass thisClass = env->GetObjectClass(thisObj);
jfieldID fidNumber = env->GetFieldID(thisClass, handleName, "J");
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;
}
#define GET_PTR(NATIVE_TYPE) getPtr<NATIVE_TYPE>(env, thisObj)
Expand Down
Loading