Android: Add scoped local ref. handler.

Adds an internal class for handling local jni references. When calling
raw jni functions the returned object is usually a local reference, to
avoid overflowing the local reference table we need to free any local
ref. as soon as we're done with them. The class QJNIScopedLocalRef,
a QScopedPointer, makes this process a lot easier.

Change-Id: I093e5671f0dcd254d5c2c7985c8c0f625d5592a4
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@theqtcompany.com>
bb10
Christian Strømme 2015-02-03 17:43:02 +01:00 committed by Christian Stromme
parent cb27388eb0
commit c8d64cbcc6
2 changed files with 17 additions and 1 deletions

View File

@ -204,6 +204,15 @@ static jfieldID getCachedFieldID(JNIEnv *env,
}
}
void QJNILocalRefDeleter::cleanup(jobject obj)
{
if (obj == 0)
return;
QJNIEnvironmentPrivate env;
env->DeleteLocalRef(obj);
}
class QJNIEnvironmentPrivateTLS
{
public:
@ -2258,4 +2267,3 @@ bool QJNIObjectPrivate::isSameObject(const QJNIObjectPrivate &other) const
}
QT_END_NAMESPACE

View File

@ -51,6 +51,14 @@
QT_BEGIN_NAMESPACE
struct Q_CORE_EXPORT QJNILocalRefDeleter
{
static void cleanup(jobject obj);
};
// To simplify this we only define it for jobjects.
typedef QScopedPointer<_jobject, QJNILocalRefDeleter> QJNIScopedLocalRef;
class Q_CORE_EXPORT QJNIEnvironmentPrivate
{
public: