B4Alpha-Aft3r0mega / javacpp

Automatically exported from code.google.com/p/javacpp
GNU General Public License v2.0
0 stars 0 forks source link

FunctionPointer call SIGSEGV when the parameter is a class not yet used in Java #5

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. protected native void call(FuncTest obj, CallbackEvent evnt);
2. don't use CallbackEvent anywhere in Java the callback is triggered
3. SIGSEGV 

What is the expected output? What do you see instead?
N/A

What version of the product are you using? On what operating system?
20111001, tested on Android 2.3.3

Please provide any additional information below.
I found a potential workaround which seems to work, but don't know the proper 
solution for the problem. If in the first loop of JNI_OnLoad, we do the 
following all the class references are loaded and we don't segfault on call.

jclass jc = JavaCPP_getClass(e, i);

Original issue reported on code.google.com by bid...@gmail.com on 17 Oct 2011 at 4:49

GoogleCodeExporter commented 8 years ago
If I understand this correctly, basically, initializing a class inside a random 
thread created outside the virtual machine crashes Android? I think that would 
be a bug of Android... If so, simply including Class.forName("CallbackEvent") 
alongside Loader.load() in your Java code should work around the issue until 
the Android team fixes it... Let me know what you think, thanks.

Original comment by samuel.a...@gmail.com on 19 Oct 2011 at 3:45

GoogleCodeExporter commented 8 years ago
I'm not 100% sure what is happening but it does look like that 
JavaCPP_getClass() is failing from the outside thread even though the call it 
done after AttachCurrentThread(). But using the work around and getting all the 
jclass from OnLoad makes everything work.

I attached the test code in case you want to try it out. It is the Android 
project but everything but the JNITestActivity.java is platform independent.

Original comment by bid...@gmail.com on 19 Oct 2011 at 3:05

Attachments:

GoogleCodeExporter commented 8 years ago
I would like to know, does calling Class.forName("CallbackEvent") alongside 
Loader.load() make everything work too?

Original comment by samuel.a...@gmail.com on 20 Oct 2011 at 7:27

GoogleCodeExporter commented 8 years ago
No, sadly that didn't work. Even when I changed it to
Class.forName("_CallbackEvent");
which is the Java class that extends Pointer. The call itself failed with class 
not found.

However, if we add 
Callback blah = Callback.ZERO;
then it works. If you look at the code, you will see ZERO is loaded from the C 
class, which forces the look up.

Original comment by bid...@gmail.com on 20 Oct 2011 at 12:14

GoogleCodeExporter commented 8 years ago
Right, you do need to specify the package namespace, something like
    Class.forName("com.unicoi.jnitest.demo.FuncTest$_CallbackEvent")

Original comment by samuel.a...@gmail.com on 20 Oct 2011 at 12:18

GoogleCodeExporter commented 8 years ago
Class.forName("com.unicoi.jnitest.demo.FuncTest$_CallbackEvent");
didn't help either. Still crashes.

Adding something like
CallbackEvent.ZERO.toString();
still makes it all happy.

I think I might use that as a work around for now.

Original comment by bid...@gmail.com on 20 Oct 2011 at 2:31

GoogleCodeExporter commented 8 years ago
Well, you should try
   Class.forName("com.unicoi.jnitest.demo.FuncTest$CallbackEvent");
If this is the class that requires initialization...

Original comment by samuel.a...@gmail.com on 21 Oct 2011 at 6:35

GoogleCodeExporter commented 8 years ago
I see, it seems like Android uses a couple of different classloaders that 
change depending on which thread we call it from, some details:
http://groups.google.com/group/android-developers/browse_thread/thread/e090b94fe
958ab31#msg_d848ae7f85bd3167
So, you were right, we should load these classes from JNI_OnLoad() if possible, 
since this does not appear like something the Android team is going to fix 
anytime soon. I implemented the workaround in the SVN repository. Let me know 
that it works for you, thanks!

Original comment by samuel.a...@gmail.com on 22 Oct 2011 at 6:00

GoogleCodeExporter commented 8 years ago
This problem has been fixed too. Thank you!

Original comment by bid...@gmail.com on 25 Oct 2011 at 2:10

GoogleCodeExporter commented 8 years ago
You are welcome!

Original comment by samuel.a...@gmail.com on 29 Oct 2011 at 5:12