Returning an int function from an inline function (C ++, jni)

Trying to figure out why calling a C ++ function returns int, the entire application crashes without any errors / warnings.

Here is the working code:

jint Java_org_ntorrent_DummyTorrentInfoProvider_next( JNIEnv * env, jobject obj, jint number) { jint test = rand(); __android_log_print(ANDROID_LOG_DEBUG, "HelloNDK!", "rand() = %d", test); return number; } 

And this code disables the application without warning:

 jint Java_org_ntorrent_DummyTorrentInfoProvider_next( JNIEnv * env, jobject obj, jint number) { jint test = rand(); __android_log_print(ANDROID_LOG_DEBUG, "HelloNDK!", "rand() = %d", test); return number + test; } 

Before the application crashes, I can see my log message (__ android_log_print) in log cat

EDIT: Even if I replace the "number + test" with "1", the application still crashes ... It only works if I return the "number" ...

EDIT # 2: Java Side Code:

 package org.ntorrent; import java.util.ArrayList; import java.util.Random; public class DummyTorrentInfoProvider implements TorrentInfoProvider { public native Integer next(Integer number); //public Integer next() { return _random.nextInt(); } public native void test(); private Random _random = new Random(100); @Override public ArrayList getTorrents() { test(); ArrayList torrents = new ArrayList(); torrents.add( new TorrentInfo("test torrent number 1", next(1), 3f, 5f)); torrents.add( new TorrentInfo("test torrent number 2", next(2), 4f, 15f)); torrents.add( new TorrentInfo("test torrent number 555")); torrents.add( new TorrentInfo("test torrent number 3", next(3), 13f, 5f)); return torrents; } static { System.loadLibrary("test"); } } 
+6
source share
1 answer
 jint Java_org_ntorrent_DummyTorrentInfoProvider_next( JNIEnv * env, jobject obj, jint number) 

and

 public native Integer next(Integer number); 

Do not match. The whole is an object, and int is a primitive.

If your own code uses jint, your Java code should use int in the declaration of the native method.

(If you want to pass Integer, you will need to process it as a job on the native side and go through the hoops to access it - it may be easier to use int / jint and perform any necessary conversion from Integer to the java side)

+13
source

Source: https://habr.com/ru/post/915706/


All Articles