Skip to content

Commit

Permalink
Clean up refs once deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
toonetown committed Mar 8, 2014
1 parent 3bcdbfd commit f7c03f3
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 57 deletions.
2 changes: 1 addition & 1 deletion core/cpp/src/main/native/c++/JClassImpl.cpp
Expand Up @@ -45,7 +45,7 @@ jclass JClassImpl::getClass() const {
}

theClass = static_cast<jclass>(env->NewGlobalRef(localClass));
env->DeleteLocalRef(localClass);
env->DeleteLocalRef(localClass), localClass = 0;
}
return theClass;
}
Expand Down
18 changes: 9 additions & 9 deletions core/cpp/src/main/native/c++/Jace.cpp
Expand Up @@ -271,7 +271,7 @@ jobject newLocalRef(jobject ref) {
void deleteLocalRef(jobject localRef) {
try {
JNIEnv* env = attach();
env->DeleteLocalRef(localRef);
env->DeleteLocalRef(localRef), localRef = 0;
} catch (...) {}
}

Expand All @@ -291,7 +291,7 @@ jobject newGlobalRef(jobject ref) {
void deleteGlobalRef(jobject globalRef) {
try {
JNIEnv* env = attach();
env->DeleteGlobalRef(globalRef);
env->DeleteGlobalRef(globalRef), globalRef = 0;
} catch (...) {}
}

Expand Down Expand Up @@ -336,7 +336,7 @@ void catchAndThrow() {
throw JNIException(msg);
}

env->DeleteLocalRef(throwableClass);
env->DeleteLocalRef(throwableClass), throwableClass = 0;
jmethodID classGetName = env->GetMethodID(classClass, "getName", "()Ljava/lang/String;");
if (!classGetName) {
string msg = "Assert failed: Unable to find the method, Class.getName().";
Expand All @@ -349,7 +349,7 @@ void catchAndThrow() {
throw JNIException(msg);
}

env->DeleteLocalRef(classClass);
env->DeleteLocalRef(classClass), classClass = 0;
jobject exceptionClass = env->CallObjectMethod(jexception, throwableGetClass);
if (env->ExceptionOccurred()) {
env->ExceptionDescribe();
Expand Down Expand Up @@ -390,8 +390,8 @@ void catchAndThrow() {
break;
}

env->DeleteLocalRef(exceptionClass);
env->DeleteLocalRef(exceptionType);
env->DeleteLocalRef(exceptionClass), exceptionClass = 0;
env->DeleteLocalRef(exceptionType), exceptionType = 0;
exceptionClass = superClass;

exceptionType = static_cast<jstring>(env->CallObjectMethod(exceptionClass, classGetName));
Expand Down Expand Up @@ -459,8 +459,8 @@ string toString(jobject obj) {

env->ReleaseStringUTFChars(javaStr, strBuf);

env->DeleteLocalRef(javaStr);
env->DeleteLocalRef(objectClass);
env->DeleteLocalRef(javaStr), javaStr = 0;
env->DeleteLocalRef(objectClass), objectClass = 0;

return value;
}
Expand All @@ -473,7 +473,7 @@ void java_throw(const std::string& internalName, const std::string& message) {
THROW_JNI_EXCEPTION("Could not find class " + internalName);
}
env->ThrowNew(exClass, message.c_str());
env->DeleteLocalRef(exClass);
env->DeleteLocalRef(exClass), exClass = 0;
}


Expand Down
4 changes: 2 additions & 2 deletions core/cpp/src/main/native/c++/proxy/JObject.cpp
Expand Up @@ -63,7 +63,7 @@ JObject::~JObject() throw ()
{
jobject ref = *this;
if (ref) {
deleteGlobalRef(ref);
deleteGlobalRef(ref), ref = 0;
}
}
catch (VirtualMachineShutdownError&)
Expand Down Expand Up @@ -159,7 +159,7 @@ void JObject::setJavaJniValue(jvalue newValue) {

// Delete the old value
if (oldValue) {
env->DeleteGlobalRef(oldValue);
env->DeleteGlobalRef(oldValue), oldValue = 0;
}
JValue::setJavaJniValue(ourCopy);
}
Expand Down
30 changes: 15 additions & 15 deletions core/cpp/src/main/native/c++/runtime/NativeProxy.cpp
Expand Up @@ -47,11 +47,11 @@ static void registerInvokeNativeHook() {
};
int methods_size = sizeof(methods) / sizeof(methods[0]);
if (env->RegisterNatives(hookClass, methods, methods_size) != JNI_OK) {
env->DeleteLocalRef(hookClass);
env->DeleteLocalRef(hookClass), hookClass = 0;
THROW_JNI_EXCEPTION("Unable to register native callback for invokeNative().");
}
registered = true;
env->DeleteLocalRef(hookClass);
env->DeleteLocalRef(hookClass), hookClass = 0;
}

/**
Expand All @@ -68,43 +68,43 @@ Builder::Builder(const std::string& className) {

m_registerCallbackMethod = env->GetMethodID(instClass, "registerNative", "(Ljava/lang/String;JI)V");
if (!m_registerCallbackMethod) {
env->DeleteLocalRef(instClass);
env->DeleteLocalRef(instClass), instClass = 0;
THROW_JNI_EXCEPTION("Assert failed: Unable to find the method, NativeInvocation.registerNative().");
}

m_createProxyMethod = env->GetMethodID(instClass, "createProxy", "()Ljava/lang/Object;");
if (!m_createProxyMethod) {
env->DeleteLocalRef(instClass);
env->DeleteLocalRef(instClass), instClass = 0;
THROW_JNI_EXCEPTION("Assert failed: Unable to find the method, NativeInvocation.createProxy().");
}

jmethodID constructor = env->GetMethodID(instClass, "<init>", "(Ljava/lang/String;)V");
if (!constructor) {
env->DeleteLocalRef(instClass);
env->DeleteLocalRef(instClass), instClass = 0;
THROW_JNI_EXCEPTION("Assert failed: Unable to find the constructor, NativeInvocation().");
}

jstring javaString = env->NewStringUTF(className.c_str());
if (!javaString) {
env->DeleteLocalRef(instClass);
env->DeleteLocalRef(instClass), instClass = 0;
THROW_JNI_EXCEPTION("Assert failed: Error creating java string.");
}

jobject instance = env->NewObject(instClass, constructor, javaString);
env->DeleteLocalRef(javaString);
env->DeleteLocalRef(javaString), javaString = 0;
if (!instance) {
env->DeleteLocalRef(instClass);
env->DeleteLocalRef(instClass), instClass = 0;
THROW_JNI_EXCEPTION("Assert failed: Error instantiating object.");
}

m_instance = env->NewGlobalRef(instance);
env->DeleteLocalRef(instance);
env->DeleteLocalRef(instance), instance = 0;
m_classRef = static_cast<jclass>(env->NewGlobalRef(instClass));
env->DeleteLocalRef(instClass);
env->DeleteLocalRef(instClass), instClass = 0;
}
Builder::~Builder() {
deleteGlobalRef(m_classRef);
deleteGlobalRef(m_instance);
deleteGlobalRef(m_classRef), m_classRef = 0;
deleteGlobalRef(m_instance), m_instance = 0;
}

void Builder::registerCallback(const std::string& name, const Callback& callback) {
Expand All @@ -122,11 +122,11 @@ void Builder::registerCallback(const std::string& name, const Callback& callback
(jint) m_callbacks.size());
string msg = "Exception thrown invoking NativeInvocation.registerNative()\n";
if (messageException(msg)) {
env->DeleteLocalRef(javaString);
env->DeleteLocalRef(javaString), javaString = 0;
throw JNIException(msg);
}
m_callbacks.push_back(callback);
env->DeleteLocalRef(javaString);
env->DeleteLocalRef(javaString), javaString = 0;
}

::jace::proxy::JObject Builder::instantiate() {
Expand All @@ -137,7 +137,7 @@ ::jace::proxy::JObject Builder::instantiate() {
}

::jace::proxy::JObject returnVal(obj);
env->DeleteLocalRef(obj);
env->DeleteLocalRef(obj), obj = 0;
return returnVal;
}

Expand Down
2 changes: 1 addition & 1 deletion core/cpp/src/main/native/include/jace/ElementProxy.h
Expand Up @@ -79,7 +79,7 @@ template <class ElementType> class ElementProxy: public virtual ::jace::proxy::J


~ElementProxy() throw () {
deleteGlobalRef(parent);
deleteGlobalRef(parent), parent = 0;
}

private:
Expand Down
24 changes: 12 additions & 12 deletions core/cpp/src/main/native/include/jace/JArray.h
Expand Up @@ -83,7 +83,7 @@ template <class ElementType> class JArray: public ::jace::proxy::JObject
{
jobject localRef = ::jace::JArrayHelper::newArray(size, ElementType::staticGetJavaJniClass());
this->setJavaJniObject(localRef);
deleteLocalRef(localRef);
deleteLocalRef(localRef), localRef = 0;
_length = size;
}

Expand Down Expand Up @@ -113,7 +113,7 @@ template <class ElementType> class JArray: public ::jace::proxy::JObject
catchAndThrow();
}
_length = values.size();
env->DeleteLocalRef(localArray);
env->DeleteLocalRef(localArray), localArray = 0;
}

JArray(const JArray& array): JObject(0)
Expand Down Expand Up @@ -168,7 +168,7 @@ template <class ElementType> class JArray: public ::jace::proxy::JObject

jvalue localElementRef = ::jace::JArrayHelper::getElement(static_cast<jobject>(*this), index);
ElementProxy<ElementType> element(this->getJavaJniArray(), localElementRef, index);
deleteLocalRef(localElementRef.l);
deleteLocalRef(localElementRef.l), localElementRef.l = 0;
return element;
}

Expand All @@ -190,7 +190,7 @@ template <class ElementType> class JArray: public ::jace::proxy::JObject

jvalue localElementRef = ::jace::JArrayHelper::getElement(static_cast<jobject>(*this), index);
ElementProxy<ElementType> element(this->getJavaJniArray(), localElementRef, index);
deleteLocalRef(localElementRef.l);
deleteLocalRef(localElementRef.l), localElementRef.l = 0;
return element;
}

Expand Down Expand Up @@ -531,7 +531,7 @@ JArray< ::jace::proxy::types::JBoolean >::JArray(int size)
}

setJavaJniObject(array);
env->DeleteLocalRef(array);
env->DeleteLocalRef(array), array = 0;

this->_length = -1;
}
Expand Down Expand Up @@ -597,7 +597,7 @@ JArray< ::jace::proxy::types::JByte >::JArray(int size)
}

setJavaJniObject(array);
env->DeleteLocalRef(array);
env->DeleteLocalRef(array), array = 0;

this->_length = -1;
}
Expand Down Expand Up @@ -662,7 +662,7 @@ JArray< ::jace::proxy::types::JChar >::JArray(int size)
}

setJavaJniObject(array);
env->DeleteLocalRef(array);
env->DeleteLocalRef(array), array = 0;

this->_length = -1;
}
Expand Down Expand Up @@ -728,7 +728,7 @@ JArray< ::jace::proxy::types::JDouble >::JArray(int size)
}

setJavaJniObject(array);
env->DeleteLocalRef(array);
env->DeleteLocalRef(array), array = 0;

this->_length = -1;
}
Expand Down Expand Up @@ -794,7 +794,7 @@ JArray< ::jace::proxy::types::JFloat >::JArray(int size)
}

setJavaJniObject(array);
env->DeleteLocalRef(array);
env->DeleteLocalRef(array), array = 0;

this->_length = -1;
}
Expand Down Expand Up @@ -860,7 +860,7 @@ JArray< ::jace::proxy::types::JInt >::JArray(int size)
}

setJavaJniObject(array);
env->DeleteLocalRef(array);
env->DeleteLocalRef(array), array = 0;

this->_length = -1;
}
Expand Down Expand Up @@ -926,7 +926,7 @@ JArray< ::jace::proxy::types::JLong >::JArray(int size)
}

setJavaJniObject(array);
env->DeleteLocalRef(array);
env->DeleteLocalRef(array), array = 0;

this->_length = -1;
}
Expand Down Expand Up @@ -992,7 +992,7 @@ JArray< ::jace::proxy::types::JShort >::JArray(int size)
}

setJavaJniObject(array);
env->DeleteLocalRef(array);
env->DeleteLocalRef(array), array = 0;

this->_length = -1;
}
Expand Down
2 changes: 1 addition & 1 deletion core/cpp/src/main/native/include/jace/JEnlister.h
Expand Up @@ -53,7 +53,7 @@ template <class T> class JEnlister : public ::jace::JFactory
{
T t(val);
// We know that val is a jobject, because you can only throw exceptions.
deleteLocalRef(val.l);
deleteLocalRef(val.l), val.l = 0;

throw t;
}
Expand Down
4 changes: 2 additions & 2 deletions core/cpp/src/main/native/include/jace/JField.h
Expand Up @@ -47,7 +47,7 @@ template <class Type> class JField
{
jvalue value = helper.getField(object);
JFieldProxy<Type> fieldProxy(helper.getFieldID(), value, object);
deleteLocalRef(value.l);
deleteLocalRef(value.l), value.l = 0;
return fieldProxy;
}

Expand All @@ -61,7 +61,7 @@ template <class Type> class JField
{
jvalue value = helper.getField(jClass);
JFieldProxy<Type> fieldProxy(helper.getFieldID(), value, jClass.getClass());
deleteLocalRef(value.l);
deleteLocalRef(value.l), value.l = 0;
return fieldProxy;
}

Expand Down
4 changes: 2 additions & 2 deletions core/cpp/src/main/native/include/jace/JFieldProxy.h
Expand Up @@ -103,12 +103,12 @@ template <class FieldType> class JFieldProxy: public FieldType
virtual ~JFieldProxy() throw () {
if (parent)
{
deleteGlobalRef(parent);
deleteGlobalRef(parent), parent = 0;
}

if (parentClass)
{
deleteGlobalRef(parentClass);
deleteGlobalRef(parentClass), parentClass = 0;
}
}

Expand Down
4 changes: 2 additions & 2 deletions core/cpp/src/main/native/include/jace/JMethod.h
Expand Up @@ -79,7 +79,7 @@ template <class ResultType> class JMethod
catchAndThrow();

ResultType result(resultRef);
env->DeleteLocalRef(resultRef);
env->DeleteLocalRef(resultRef), resultRef = 0;

return result;
}
Expand Down Expand Up @@ -110,7 +110,7 @@ template <class ResultType> class JMethod
catchAndThrow();

ResultType result(resultRef);
env->DeleteLocalRef(resultRef);
env->DeleteLocalRef(resultRef), resultRef = 0;

return result;
}
Expand Down

0 comments on commit f7c03f3

Please sign in to comment.