Android: Fix Wii Remote connection crashes

Users are reporting a crash at the point where WiimoteAndroid::IORead
tries to use m_java_wiimote_payload. This commit solves the problem by
making m_java_wiimote_payload a global reference.

The code for setting up m_java_wiimote_payload has also been moved to
the constructor just because that way it's impossible for it to run
twice. (If the code as written were to run a second time, the old global
reference would be leaked. ConnectInternal should only run once, so this
is just to be on the safe side.)

Fixes https://bugs.dolphin-emu.org/issues/13960.
This commit is contained in:
JosJuice
2026-02-28 16:10:58 +01:00
parent f898d75bf3
commit faa861f1ab
+14 -11
View File
@@ -58,11 +58,25 @@ auto WiimoteScannerAndroid::FindAttachedWiimotes() -> FindResults
WiimoteAndroid::WiimoteAndroid(int index) : Wiimote(), m_mayflash_index(index)
{
auto* const env = IDCache::GetEnvForThread();
jfieldID payload_field = env->GetStaticFieldID(s_adapter_class, "wiimotePayload", "[[B");
jobjectArray payload_object =
reinterpret_cast<jobjectArray>(env->GetStaticObjectField(s_adapter_class, payload_field));
jobject java_wiimote_payload = env->GetObjectArrayElement(payload_object, m_mayflash_index);
m_java_wiimote_payload = reinterpret_cast<jbyteArray>(env->NewGlobalRef(java_wiimote_payload));
// Get function pointers
m_input_func = env->GetStaticMethodID(s_adapter_class, "input", "(I)I");
m_output_func = env->GetStaticMethodID(s_adapter_class, "output", "(I[BI)I");
}
WiimoteAndroid::~WiimoteAndroid()
{
Shutdown();
auto* const env = IDCache::GetEnvForThread();
env->DeleteGlobalRef(m_java_wiimote_payload);
}
std::string WiimoteAndroid::GetId() const
@@ -81,17 +95,6 @@ bool WiimoteAndroid::ConnectInternal()
if (IsConnected())
return true;
auto* const env = IDCache::GetEnvForThread();
jfieldID payload_field = env->GetStaticFieldID(s_adapter_class, "wiimotePayload", "[[B");
jobjectArray payload_object =
reinterpret_cast<jobjectArray>(env->GetStaticObjectField(s_adapter_class, payload_field));
m_java_wiimote_payload = (jbyteArray)env->GetObjectArrayElement(payload_object, m_mayflash_index);
// Get function pointers
m_input_func = env->GetStaticMethodID(s_adapter_class, "input", "(I)I");
m_output_func = env->GetStaticMethodID(s_adapter_class, "output", "(I[BI)I");
// Test a write to see if a remote is actually connected to the DolphinBar.
constexpr u8 report[] = {WR_SET_REPORT | BT_OUTPUT,
u8(WiimoteCommon::OutputReportID::RequestStatus), 0};