java 从本机函数(c++,jni)返回 int 会导致应用程序崩溃

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10588665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:42:25  来源:igfitidea点击:

Returning an int from native function(c++, jni) crashes application

javaandroidc++android-ndkjava-native-interface

提问by 6opuc

Trying to figure out why c++ function call returning an int crashes the whole application without any errors/warnings.

试图弄清楚为什么返回 int 的 c++ 函数调用会使整个应用程序崩溃而没有任何错误/警告。

Here is 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 crashes application without warnings:

并且此代码在没有警告的情况下使应用程序崩溃:


    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

在应用程序崩溃之前,我可以在 log cat 中看到我的日志消息(__android_log_print)

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

编辑:即使我用“1”替换“数字+测试”,应用程序仍然崩溃......它只有在我返回“数字”时才有效......

EDIT#2: Java-side code:

编辑#2:Java 端代码:


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");
    }
}

回答by Chris Stratton

jint Java_org_ntorrent_DummyTorrentInfoProvider_next(
    JNIEnv * env, jobject obj, jint number)

and

public native Integer next(Integer number);

Do not match. An Integeris an Object, while an intis a primitive.

不匹配。一个整数是一个对象,而一个INT是一个原语。

If your native code uses jint, your java code should use intin the declaration of the native method.

如果您的本机代码使用jint,则您的 java 代码应在本机方法的声明中使用int

(If you wish to pass an Integer, you'll need to treat it as a jobject on the native side, and jump through hoops to access it - it's probably easier to use int/jint and do any necessary conversion to from Integer in the java side)

(如果你想传递一个整数,你需要把它当作本地的一个作业,并跳过箍来访问它 - 使用 int/jint 可能更容易,并在java端)