Skip to content

Commit 98b8d0f

Browse files
committed
Suppress stdout for failed to attach threads on jvm shutdown
Signed-off-by: Joshua Castle <26531652+Kas-tle@users.noreply.github.com>
1 parent b79ef64 commit 98b8d0f

4 files changed

Lines changed: 33 additions & 32 deletions

File tree

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version=1.0.3
1+
version=1.0.4

webrtc-jni/src/main/cpp/dependencies/jni-voithos/include/JavaThreadEnv.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ namespace jni
2323
private:
2424
JavaVM * vm;
2525
JNIEnv * env;
26+
bool attached;
2627
};
2728
}
2829

webrtc-jni/src/main/cpp/dependencies/jni-voithos/src/JavaThreadEnv.cpp

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,33 @@
77

88
#include "JavaThreadEnv.h"
99

10-
#include <iostream>
11-
#include <thread>
12-
1310
namespace jni
1411
{
15-
JavaThreadEnv::JavaThreadEnv(JavaVM * vm) :
16-
vm(vm),
17-
env(nullptr)
18-
{
19-
int status = vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6);
20-
21-
if (status == JNI_EDETACHED) {
22-
if (vm->AttachCurrentThread(reinterpret_cast<void**>(&env), NULL) != 0) {
23-
std::cout << "VM attach current thread failed" << std::endl;
24-
}
25-
}
26-
27-
if (env == nullptr) {
28-
std::cout << "Failed to attach thread " << std::this_thread::get_id() << std::endl;
29-
}
30-
}
31-
32-
JavaThreadEnv::~JavaThreadEnv()
33-
{
34-
vm->DetachCurrentThread();
35-
36-
//std::cout << "Dettached thread " << std::this_thread::get_id() << std::endl;
37-
}
38-
39-
JNIEnv * JavaThreadEnv::getEnv() const
40-
{
41-
return env;
42-
}
12+
JavaThreadEnv::JavaThreadEnv(JavaVM * vm) :
13+
vm(vm),
14+
env(nullptr),
15+
attached(false)
16+
{
17+
int status = vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6);
18+
19+
if (status == JNI_EDETACHED) {
20+
if (vm->AttachCurrentThread(reinterpret_cast<void**>(&env), NULL) == JNI_OK) {
21+
attached = true;
22+
} else {
23+
env = nullptr;
24+
}
25+
}
26+
}
27+
28+
JavaThreadEnv::~JavaThreadEnv()
29+
{
30+
if (attached) {
31+
vm->DetachCurrentThread();
32+
}
33+
}
34+
35+
JNIEnv * JavaThreadEnv::getEnv() const
36+
{
37+
return env;
38+
}
4339
}

webrtc-jni/src/main/cpp/src/rtc/LogSink.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ namespace jni
3535
{
3636
JNIEnv * env = AttachCurrentThread();
3737

38+
if (env == nullptr) {
39+
return;
40+
}
41+
3842
JavaLocalRef<jobject> jSeverity = JavaEnums::toJava(env, severity);
3943
JavaLocalRef<jstring> jMessage = JavaString::toJava(env, message);
4044

0 commit comments

Comments
 (0)