Skip to content

Commit

Permalink
draft fix for sound end callback native memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
fgnm committed Oct 27, 2024
1 parent 9bf78c2 commit 1b6f17f
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/main/java/games/rednblack/miniaudio/MiniAudio.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uin
static jmethodID jon_native_sound_end;
static jmethodID jon_native_log;
static jmethodID jon_native_notification;
static JNIEnv* soundEnv;
// Helper macro for platform-specific thread attachment
#ifdef MA_ANDROID
Expand All @@ -103,9 +104,15 @@ void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uin
}
void sound_end_callback(void* pUserData, ma_sound* pSound) {
ATTACH_ENV()
env->CallVoidMethod(jMiniAudio, jon_native_sound_end, (jlong) pSound);
DETACH_ENV()
if (jvm->GetEnv((void**)&soundEnv, JNI_VERSION_1_6) == JNI_EDETACHED) {
#ifdef MA_ANDROID
jvm->AttachCurrentThread(&soundEnv, NULL);
#else
jvm->AttachCurrentThread((void**)&soundEnv, NULL);
#endif
}
soundEnv->CallVoidMethod(jMiniAudio, jon_native_sound_end, (jlong) pSound);
}
void ma_log_callback_jni(void* pUserData, ma_uint32 level, const char* pMessage) {
Expand All @@ -120,6 +127,13 @@ void notification_callback_jni(const ma_device_notification* pNotification) {
ATTACH_ENV()
env->CallVoidMethod(jMiniAudio, jon_native_notification, pNotification->type);
DETACH_ENV()
if (pNotification->type == ma_device_notification_type_stopped) {
JNIEnv* env;
if (jvm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_EDETACHED) {
jvm->DetachCurrentThread();
}
}
}
*/

Expand Down

0 comments on commit 1b6f17f

Please sign in to comment.