SEGV_ACCERR issue in java plugin

Hi there, recently I am experiencing SIGSEGV and SEGV_ACCERR issue in my Unity3D project.

My Unity project is deployed in Android 2.3.1 platform with a java library plug-in. But each time the native method is called, the logcat shows following log messages and then application is shut down:

12-08 15:40:31.729: I/DEBUG(1238): Build fingerprint: 'hutch_aus/htc_vision/vision:2.3.3/GRI40/91002:user/release-keys'
12-08 15:40:31.729: I/DEBUG(1238): pid: 29586, tid: 29588  >>> com.mdg.android.unity3dtext <<<
12-08 15:40:31.729: I/DEBUG(1238): signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 450a89e8
12-08 15:40:31.729: I/DEBUG(1238):  r0 00001000  r1 005f8a98  r2 00000000  r3 800a7678
12-08 15:40:31.729: I/DEBUG(1238):  r4 fffffe78  r5 42091000  r6 00c05e7a  r7 460e9ec8
12-08 15:40:31.729: I/DEBUG(1238):  r8 800903fd  r9 00000008  10 0059eb96  fp 0000000a
12-08 15:40:31.729: I/DEBUG(1238):  ip 00001000  sp 44e9bd2c  lr 80052dc3  pc 80035f18  cpsr 40000010
12-08 15:40:31.729: I/DEBUG(1238):  d0  0000000000681300  d1  4051704800323c61
12-08 15:40:31.729: I/DEBUG(1238):  d2  002e00700070006c  d3  0069007400630020
12-08 15:40:31.729: I/DEBUG(1238):  d4  00300031002c0035  d5  0033002c00370030
12-08 15:40:31.729: I/DEBUG(1238):  d6  005b1fe000300030  d7  000000374d9cfcc5
12-08 15:40:31.729: I/DEBUG(1238):  d8  000039c855517bdf  d9  0000000000000000
12-08 15:40:31.729: I/DEBUG(1238):  d10 0000000000000000  d11 0000000000000000
12-08 15:40:31.729: I/DEBUG(1238):  d12 0000000000000000  d13 0000000000000000
12-08 15:40:31.729: I/DEBUG(1238):  d14 0000000000000000  d15 0000000000000000
12-08 15:40:31.729: I/DEBUG(1238):  d16 0000000000000000  d17 3ff0000000000000
12-08 15:40:31.729: I/DEBUG(1238):  d18 a2a2a2a2a2bacdcd  d19 cdcdcdcdbea3a2a2
12-08 15:40:31.729: I/DEBUG(1238):  d20 4008000000000000  d21 0000000000000000
12-08 15:40:31.729: I/DEBUG(1238):  d22 00000000000000a2  d23 3fcc7288e957b53b
12-08 15:40:31.729: I/DEBUG(1238):  d24 3fc74721cad6b0ed  d25 3fc39a09d078c69f
12-08 15:40:31.729: I/DEBUG(1238):  d26 0000000000000000  d27 0000000000000000
12-08 15:40:31.729: I/DEBUG(1238):  d28 0000000000000000  d29 0000000000000000
12-08 15:40:31.729: I/DEBUG(1238):  d30 0000000000000000  d31 0000000000000000
12-08 15:40:31.729: I/DEBUG(1238):  scr 80000012

Here is my c# script of calling jni function:

using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
using System;

public class CallJavaCode : MonoBehaviour {
	[DllImport("javabridge")]
	private static extern void testFunction();

	private string cacheDir = "Click Me!";
	void OnGUI ()
	{
		if (GUI.Button(new Rect (15, 125, 450, 100), cacheDir))
		{
			testFunction();
		}
	}
}

The jni function in *.so looks like this:

void testFunction()
{
	JNIEnv* jni_env = 0;
	java_vm->AttachCurrentThread(&jni_env, 0);
	__android_log_print(ANDROID_LOG_INFO, "JavaBridge", "[%s] called, attached to %08x

", FUNCTION, jni_env);

    jni_env->CallObjectMethod(JavaClass, testMethodId);
}

Here is my java library code in testMethod:

public void testMethod(){	
		HttpParams params = new BasicHttpParams();
		HttpPost httppost = new HttpPost("some url ");
		HttpResponse response;
		
		params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
		HttpClient httpclient = new DefaultHttpClient(params);
		
		try {
			response = httpclient.execute(httppost);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
		} catch (IOException e) {
			// TODO Auto-generated catch block
		}
}

I am aware that the application crashes in code “response = httpclient.execute(httppost);”

So is it because there might be compatible issue between Unity3d and Android platform in httpclient.execute() or just something wrong with jni?

Can someone help?

It’s hard to say exactly where the problem is - the crash output is incomplete, and there could be possible hints just before the output above (if debug.checkjni is set), and right after you usually see the callstack etc.

If we assume that you get the __android_log_print output in the logcat, then the params to CallObjectMethod() could be invalid (there is no code showing how those are fetched).

But I would suggest to use the AndroidJavaObject et. al. if you only care about calling Java code from C# code - instead of writing the native bridge yourself. The code would then look something like:

using(AndroidJavaObject javaClass = new AndroidJavaObject("java.class.name.here"))
{
    javaClass.Call("testMethod");
}

and only keep the .jar with your Java code in the plugins folder (there is no need for that libjavabridge.so then).