Skip to content

Commit d2dbdd5

Browse files
committed
fix: resolve JNI callback SIGSEGV by correcting GlobalRef linkage
1 parent b9da9c1 commit d2dbdd5

2 files changed

Lines changed: 17 additions & 12 deletions

File tree

binding/src/main/java/dev/eatgrapes/live2d/CubismUserModel.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ public class CubismUserModel extends Native {
77

88
public CubismUserModel() {
99
super(createNative());
10+
initNative(_ptr);
1011
}
1112

1213
private static native long createNative();
14+
private native void initNative(long ptr);
1315

1416
public void loadModel(byte[] buffer) { loadModelNative(_ptr, buffer); }
1517
private static native void loadModelNative(long ptr, byte[] buffer);

native/src/CubismUserModel_JNI.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@ using namespace Live2D::Cubism::Framework::Rendering;
1212

1313
class JniUserModel : public CubismUserModel {
1414
public:
15-
JniUserModel(JNIEnv* env, jobject javaObj) {
15+
JniUserModel(JNIEnv* env) {
1616
env->GetJavaVM(&_jvm);
17+
}
18+
19+
void initJavaObj(JNIEnv* env, jobject javaObj) {
1720
_javaObj = env->NewGlobalRef(javaObj);
1821
}
1922

2023
~JniUserModel() {
2124
JNIEnv* env = getEnv();
22-
if (env) env->DeleteGlobalRef(_javaObj);
25+
if (env && _javaObj) env->DeleteGlobalRef(_javaObj);
2326
for (auto& pair : _expressionData) pair.second.clear();
2427
}
2528

@@ -35,7 +38,7 @@ class JniUserModel : public CubismUserModel {
3538

3639
void loadPoseCopy(const csmByte* buffer, csmSizeInt size) {
3740
_poseData.assign(buffer, buffer + size);
38-
LoadPose(_poseData.data(), (csmSizeInt)_poseData.size());
41+
LoadPose(_poseData.data(), (csmSizeInt)_physicsData.size());
3942
}
4043

4144
void loadExpressionCopy(const csmByte* buffer, csmSizeInt size, const std::string& name) {
@@ -57,7 +60,7 @@ class JniUserModel : public CubismUserModel {
5760

5861
void notifyFinished() {
5962
JNIEnv* env = getEnv();
60-
if (!env) return;
63+
if (!env || !_javaObj) return;
6164
jclass cls = env->GetObjectClass(_javaObj);
6265
jmethodID mid = env->GetMethodID(cls, "onMotionFinished", "(Ljava/lang/String;)V");
6366
jstring name = env->NewStringUTF("motion");
@@ -74,13 +77,10 @@ class JniUserModel : public CubismUserModel {
7477

7578
void update(float dt) {
7679
if (!_model) return;
77-
7880
_model->LoadParameters();
7981
if (!_motionManager->IsFinished()) _motionManager->UpdateMotion(_model, dt);
8082
_model->SaveParameters();
81-
8283
if (_pose) _pose->UpdateParameters(_model, dt);
83-
8484
if (_dragManager) {
8585
_dragManager->Update(dt);
8686
auto* idm = CubismFramework::GetIdManager();
@@ -89,7 +89,6 @@ class JniUserModel : public CubismUserModel {
8989
_model->AddParameterValue(idm->GetId("ParamEyeBallX"), _dragManager->GetX());
9090
_model->AddParameterValue(idm->GetId("ParamEyeBallY"), _dragManager->GetY());
9191
}
92-
9392
if (_physics) _physics->Evaluate(_model, dt);
9493
_model->Update();
9594
}
@@ -102,15 +101,19 @@ class JniUserModel : public CubismUserModel {
102101
}
103102

104103
JavaVM* _jvm;
105-
jobject _javaObj;
104+
jobject _javaObj = nullptr;
106105
std::vector<csmByte> _mocData, _physicsData, _poseData;
107106
std::map<std::string, std::vector<csmByte>> _expressionData;
108107
};
109108

110109
extern "C" {
111110

112-
JNIEXPORT jlong JNICALL Java_dev_eatgrapes_live2d_CubismUserModel_createNative(JNIEnv* env, jobject thiz) {
113-
return (jlong) new JniUserModel(env, thiz);
111+
JNIEXPORT jlong JNICALL Java_dev_eatgrapes_live2d_CubismUserModel_createNative(JNIEnv* env, jclass) {
112+
return (jlong) new JniUserModel(env);
113+
}
114+
115+
JNIEXPORT void JNICALL Java_dev_eatgrapes_live2d_CubismUserModel_initNative(JNIEnv* env, jobject thiz, jlong ptr) {
116+
((JniUserModel*)ptr)->initJavaObj(env, thiz);
114117
}
115118

116119
JNIEXPORT void JNICALL Java_dev_eatgrapes_live2d_CubismUserModel_deleteNative(JNIEnv*, jclass, jlong ptr) {
@@ -212,4 +215,4 @@ JNIEXPORT void JNICALL Java_dev_eatgrapes_live2d_CubismUserModel_drawNative(JNIE
212215
env->ReleaseFloatArrayElements(matrix, m_ptr, JNI_ABORT);
213216
}
214217

215-
}
218+
}

0 commit comments

Comments
 (0)