簡體   English   中英

使用Android NDK C ++時出現致命信號11(SIGSEGV),代碼1錯誤

[英]Fatal signal 11 (SIGSEGV), code 1 error when using Android NDK C++

制作通過NDK同時使用Java和C ++代碼的Android應用程序時出現以下錯誤。 具體來說,我嘗試了許多有效的輸入(這是一個計算器應用程序),它們都會導致此錯誤,但是例如,我嘗試的輸入是4 + 3-5

05-03 00:54:05.676 30652-30652/com.x10host.dhanushpatel.nativecalc A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0xd89f95b0 in tid 30652 (atel.nativecalc)
05-03 00:54:06.462 30652-30652/com.x10host.dhanushpatel.nativecalc W/atel.nativecalc: type=1701 audit(0.0:1454): auid=4294967295 uid=10192 gid=10192 ses=4294967295 subj=u:r:untrusted_app:s0 reason="memory violation" sig=11

我不確定我的錯誤是什么或在哪里或如何解決。

這是我的C ++代碼:

#include <jni.h>
#include <stack>
#include <string>
#include <cctype>
#include <sstream>
#include <stdlib.h>

#include <android/log.h>

using namespace std;
stack<string> operationsStack;
stack<double> numbersStack;
int arraySize = 0;

extern "C" {


void doOperation() {

    string oString = operationsStack.top();
    operationsStack.pop();

    double a = numbersStack.top();
    numbersStack.pop();
    double b = numbersStack.top();
    numbersStack.pop();

    if (oString == "x") {
        numbersStack.push(a * b);
    }
    else if (oString == "/") {
        numbersStack.push(b/a);
    }
    else if (oString == "+") {
        numbersStack.push(a + b);
    }
    else {
        numbersStack.push(b-a);
    }

}

double calcExpression(string calcArray[]) {

    for (int i = 0; i < arraySize; i++) {
        string chr = calcArray[i];
        if (chr == "(") {
        }
        else if (chr == "+" || chr == "x" || chr == "-" || chr == "/") {
            operationsStack.push(chr);
        }
        else if (chr == ")") {
            doOperation();
        }
        else {
            numbersStack.push(atof(chr.c_str()));
        }
    }
    doOperation();

    double result = numbersStack.top();
    numbersStack.pop();
    return result;
}

JNIEXPORT jstring JNICALL
Java_com_x10host_dhanushpatel_nativecalc_MainActivity_calcPrint
        (JNIEnv *env, jobject jot, jobjectArray calcArray, jint aSize) {

    arraySize = aSize;

    string cppArray[arraySize];
    for (int i = 0; i < arraySize; i++) {
        jstring string = (jstring) ((env)->GetObjectArrayElement(calcArray, i));
        const char *rawString = env->GetStringUTFChars(string, 0);
        cppArray[i] = rawString;
    }
    ostringstream oss;
    oss << calcExpression(cppArray);

    return env->NewStringUTF(oss.str().c_str());


}

JNIEXPORT void JNICALL
Java_com_x10host_dhanushpatel_nativecalc_MainActivity_calcCClear(JNIEnv *env) {
    while (!operationsStack.empty()) {
        operationsStack.pop();
    }
    while (!numbersStack.empty()) {
        numbersStack.pop();
    }
}

}

這是我的Java代碼:

package com.x10host.dhanushpatel.nativecalc;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private native String calcPrint(String[] jCL,int alLength);
    private native void calcCClear();
    ArrayList<String> jCalcList = new ArrayList<>();
    TextView calcShow;

    static {
        System.loadLibrary("cplusplus11");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        calcShow = (TextView) findViewById(R.id.calcShow);

    }

    public void calcAdd(View v) {
        Button b = (Button) v;
        String buttonText = b.getText().toString();
        Log.i("button pressed: ",buttonText);
        jCalcList.add(buttonText);
        calcShow.setText(calcShow.getText().toString()+buttonText);
    }

    public void calcSum(View v){
        int jclSize = jCalcList.size();
        String[] jCalcArray = new String[jclSize];
        for(int i =0;i<jclSize;i++){
            jCalcArray[i]=jCalcList.get(i);
        }
        calcShow.setText(calcPrint(jCalcArray,jCalcArray.length));
    }

    public void calcClear(View v){
        calcCClear();
        calcShow.setText("");
    }

}

幫助將不勝感激。 提前致謝!

我已經解決了我的問題,並且我的代碼不再提供任何錯誤,並且我的應用程序不再崩潰。 現在,代碼的邏輯有效地起作用了。 某些功能仍需要添加/更改,但這些功能與我在這里提到的問題無關。

編輯:對我的代碼進行的更改(可能已修復)包括:正確設置/獲取calcSum中循環的最大值,使用double變量存儲double結果而不是int(在C ++代碼中的某個地方),並且可能還有其他變化。 我意識到的另一件事是,我的代碼僅對括號內的表達式求值,這不是程序的錯誤,錯誤在於我對代碼邏輯的假設。

簡介:如果在這種情況下發生此錯誤,則您可能需要修復代碼,程序在技術上可以按照您的代碼運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM