Tuesday, April 16, 2013

[android help] Unexpected crash using jni in monodroid project


I've been trying to import a simple hello world NDK project into a monodroid project using Xamarin Studio.


The NDK part of the project compiles and builds fine, I can invoke the native methods but when I try to access the JNIEnv the app crashes with a SIGSEGV, see below for console output and relevant code snips.



Very basic console logging
Starting method
Accessing env
Stacktrace:

at <0xffffffff>
at (wrapper managed-to-native) BasicNDK.Activity1.LogNdk (string)
at BasicNDK.Activity1.b__0 (object,System.EventArgs) [0x00023] in c:\Users\cbramley\Documents\Visual Studio 2012\Projects\AndroidApplication1\BasicNDK\MainActivity.cs:56
at Android.Views.View/IOnClickListenerImplementor.OnClick (Android.Views.View) [0x0000c] in /Users/builder/data/lanes/monodroid-mlion-master/bf2b736d/source/monodroid/src/Mono.Android/platforms/android-10/src/generated/Android.Views.View.cs:643
at Android.Views.View/IOnClickListenerInvoker.n_OnClick_Landroid_view_View_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/monodroid-mlion-master/bf2b736d/source/monodroid/src/Mono.Android/platforms/android-10/src/generated/Android.Views.View.cs:614
at (wrapper dynamic-method) object.6ea2e501-d56c-455b-9c13-849da747461e (intptr,intptr,intptr)
at (wrapper native-to-managed) object.6ea2e501-d56c-455b-9c13-849da747461e (intptr,intptr,intptr)

=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries
used by your application.
=================================================================


The monodroid activity code:



// snipped rest of activity
[DllImport ("ndksample")]
static extern void LogNdk ( string w );

[DllImport ("ndksample")]
static extern void LogNdkDefaultMessage ();

protected override void OnCreate ( Bundle bundle )
{
base.OnCreate ( bundle );
SetContentView ( Resource.Layout.Main );

Button button = FindViewById

Finally the NDK implementation:



#include
#include
#include

#define DEBUG_TAG "NDK_AndroidNDK1SampleActivity"

void LogNdk(JNIEnv * env, jobject this, jstring logThis)
{
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "Starting method");
jboolean isCopy;
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "Accessing env");
const char * szLogThis = (*env)->GetStringUTFChars(env, logThis, &isCopy);
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:LC: [%s]", szLogThis);
(*env)->ReleaseStringUTFChars(env, logThis, szLogThis);
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "Finished");
}

void LogNdkDefaultMessage(JNIEnv * env, jobject this)
{
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "Very basic console logging");
}


I've tracked the problem down to a crash on this line const char * szLogThis = (*env)->GetStringUTFChars(env, logThis, &isCopy); but I don't understand what's causing it, can anyone help explain it to me please? Or better yet tell me how to fix it :)



.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...