Index: jni/Utils.h |
diff --git a/jni/Utils.h b/jni/Utils.h |
index 852457cdd95095e57e8232fe2b40ae316814c636..93113bdcc4f397174fac292f87c5aead9c52fcf1 100644 |
--- a/jni/Utils.h |
+++ b/jni/Utils.h |
@@ -19,6 +19,7 @@ |
#define UTILS_H |
#include <string> |
+#include <vector> |
#include <algorithm> |
#include <jni.h> |
#include <stdexcept> |
@@ -39,6 +40,43 @@ void JniThrowException(JNIEnv* env, const std::exception& e); |
void JniThrowException(JNIEnv* env); |
+class JNIEnvAcquire; |
+ |
+/** |
+ * This class is _NOT_ thread safe! |
+ */ |
+template<typename T> |
+class JniLocalReference |
+{ |
+public: |
+ explicit JniLocalReference(JNIEnv* jniEnv, T object) |
+ : m_JNIEnv(jniEnv), m_Object(object) |
+ { |
+ |
+ } |
+ |
+ JniLocalReference(const JniLocalReference<T>& other); |
+ |
+ ~JniLocalReference() |
+ { |
+ m_JNIEnv->DeleteLocalRef(m_Object); |
+ } |
+ |
+ T operator*() |
+ { |
+ return m_Object; |
+ } |
+ |
+ T Get() |
+ { |
+ return m_Object; |
+ } |
+ |
+private: |
+ JNIEnv* m_JNIEnv; |
+ T m_Object; |
+}; |
+ |
class JNIEnvAcquire |
{ |
public: |
@@ -145,9 +183,9 @@ inline jobject NewJniFilter(JNIEnv* env, const AdblockPlus::FilterPtr& filter) |
return 0; |
} |
- jclass clazz = env->FindClass(PKG("Filter")); |
- jmethodID method = env->GetMethodID(clazz, "<init>", "(J)V"); |
- return env->NewObject(clazz, method, JniPtrToLong(new AdblockPlus::FilterPtr(filter))); |
+ JniLocalReference<jclass> clazz(env, env->FindClass(PKG("Filter"))); |
+ jmethodID method = env->GetMethodID(*clazz, "<init>", "(J)V"); |
+ return env->NewObject(*clazz, method, JniPtrToLong(new AdblockPlus::FilterPtr(filter))); |
} |
inline jobject NewJniSubscription(JNIEnv* env, const AdblockPlus::SubscriptionPtr& subscription) |
@@ -157,9 +195,9 @@ inline jobject NewJniSubscription(JNIEnv* env, const AdblockPlus::SubscriptionPt |
return 0; |
} |
- jclass clazz = env->FindClass(PKG("Subscription")); |
- jmethodID method = env->GetMethodID(clazz, "<init>", "(J)V"); |
- return env->NewObject(clazz, method, JniPtrToLong(new AdblockPlus::SubscriptionPtr(subscription))); |
+ JniLocalReference<jclass> clazz(env, env->FindClass(PKG("Subscription"))); |
+ jmethodID method = env->GetMethodID(*clazz, "<init>", "(J)V"); |
+ return env->NewObject(*clazz, method, JniPtrToLong(new AdblockPlus::SubscriptionPtr(subscription))); |
} |
#define CATCH_AND_THROW(jEnv) \ |