Skip to content

Commit

Permalink
Add macros to load classes and store as weak reference (#14)
Browse files Browse the repository at this point in the history
Motivation:

When classes are loaded and stored via a global reference we might end
up with issues when we try to unload the classloader due of circlar
references.

Modifications:

Add macros to load and unload classes and store these as weak references

Result:

Preparation to fix netty/netty#13480
  • Loading branch information
normanmaurer committed Jul 19, 2023
1 parent f2b7379 commit b77d860
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/main/c/netty_jni_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,28 @@
} \
NETTY_JNI_UTIL_END_MACRO

#define NETTY_JNI_UTIL_LOAD_CLASS_WEAK(E, C, N, R) \
NETTY_JNI_UTIL_BEGIN_MACRO \
jclass _##C = (*(E))->FindClass((E), N); \
if (_##C == NULL) { \
(*(E))->ExceptionClear((E)); \
goto R; \
} \
C = (*(E))->NewWeakGlobalRef((E), _##C); \
(*(E))->DeleteLocalRef((E), _##C); \
if (C == NULL) { \
goto R; \
} \
NETTY_JNI_UTIL_END_MACRO

#define NETTY_JNI_UTIL_UNLOAD_CLASS_WEAK(E, C) \
NETTY_JNI_UTIL_BEGIN_MACRO \
if (C != NULL) { \
(*(E))->DeleteWeakGlobalRef((E), (C)); \
C = NULL; \
} \
NETTY_JNI_UTIL_END_MACRO


#define NETTY_JNI_UTIL_GET_METHOD(E, C, M, N, S, R) \
NETTY_JNI_UTIL_BEGIN_MACRO \
Expand Down

0 comments on commit b77d860

Please sign in to comment.