@@ -68,7 +68,13 @@ public:
68
68
template <class Derived , const char * FqName>
69
69
class JvmInstanceWrapper {
70
70
protected:
71
- bool is_weak = false ;
71
+ enum RefType {
72
+ STRONG,
73
+ WEAK,
74
+ DEAD
75
+ };
76
+
77
+ RefType ref_type = STRONG;
72
78
jni::JObject wrapped;
73
79
74
80
explicit JvmInstanceWrapper (jni::Env& p_env, jni::JObject p_wrapped);
@@ -79,6 +85,7 @@ class JvmInstanceWrapper {
79
85
const jni::JObject& get_wrapped () const ;
80
86
void swap_to_strong_unsafe (jni::Env& p_env);
81
87
void swap_to_weak_unsafe (jni::Env& p_env);
88
+ void invalidate_ref (jni::Env& p_env);
82
89
83
90
static bool initialize (jni::Env& p_env, ClassLoader* class_loader);
84
91
static Derived* create_instance (jni::Env& p_env, ClassLoader* class_loader);
@@ -119,16 +126,12 @@ void JvmInstanceWrapper<Derived, FqName>::finalize(jni::Env& p_env, ClassLoader*
119
126
template <class Derived , const char * FqName>
120
127
JvmInstanceWrapper<Derived, FqName>::~JvmInstanceWrapper () {
121
128
jni::Env env {jni::Jvm::current_env ()};
122
- if (is_weak) {
123
- wrapped.delete_weak_ref (env);
124
- } else {
125
- wrapped.delete_global_ref (env);
126
- }
129
+ invalidate_ref (env);
127
130
}
128
131
129
132
template <class Derived , const char * FqName>
130
133
bool JvmInstanceWrapper<Derived, FqName>::is_ref_weak() const {
131
- return is_weak ;
134
+ return ref_type == WEAK ;
132
135
}
133
136
134
137
template <class Derived , const char * FqName>
@@ -137,7 +140,7 @@ void JvmInstanceWrapper<Derived, FqName>::swap_to_strong_unsafe(jni::Env& p_env)
137
140
jni::JObject new_ref = wrapped.new_global_ref <jni::JObject>(p_env);
138
141
wrapped.delete_weak_ref (p_env);
139
142
wrapped = new_ref;
140
- is_weak = false ;
143
+ ref_type = STRONG ;
141
144
}
142
145
143
146
template <class Derived , const char * FqName>
@@ -146,12 +149,27 @@ void JvmInstanceWrapper<Derived, FqName>::swap_to_weak_unsafe(jni::Env& p_env) {
146
149
jni::JObject new_ref = wrapped.new_weak_ref <jni::JObject>(p_env);
147
150
wrapped.delete_global_ref (p_env);
148
151
wrapped = new_ref;
149
- is_weak = true ;
152
+ ref_type = WEAK;
153
+ }
154
+
155
+ template <class Derived , const char * FqName>
156
+ void JvmInstanceWrapper<Derived, FqName>::invalidate_ref(jni::Env& p_env) {
157
+ switch (ref_type) {
158
+ case STRONG:
159
+ wrapped.delete_global_ref (p_env);
160
+ break ;
161
+ case WEAK:
162
+ wrapped.delete_weak_ref (p_env);
163
+ break ;
164
+ default :
165
+ break ;
166
+ }
167
+ ref_type = DEAD;
150
168
}
151
169
152
170
template <class Derived , const char * FqName>
153
171
const jni::JObject& JvmInstanceWrapper<Derived, FqName>::get_wrapped() const {
154
172
return wrapped;
155
173
}
156
174
157
- #endif // GODOT_JVM_JVM_INSTANCE_WRAPPER_H
175
+ #endif // GODOT_JVM_JVM_INSTANCE_WRAPPER_H
0 commit comments