0

我正在编写一个需要路径名才能最终创建全局字符串的程序。我目前有这个路径名硬编码,但想使用一个全局变量来替换它。我遇到的问题是我的全局变量未定义。我也在使用 JNI,我得到的错误是:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00007f65f981ddd0, pid=11660, tid=140075985102592   
#
# JRE version: 7.0_21-b02
# Java VM: OpenJDK 64-Bit Server VM (23.7-b01 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# C  [libstdc++.so.6+0x9ddd0]  std::string::size() const+0x0
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /home/cbil/Desktop/SIGH_Project/July_9/src/hs_err_pid11660.log
#
# If you would like to submit a bug report, please include
# instructions on how to reproduce the bug and visit:
#   https://bugs.launchpad.net/ubuntu/+source/openjdk-7/
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

以下是为简单起见重新格式化的相关代码:

文件1.cpp

#include <jni.h>
#include "file1.h"
#include <iostream>
#include <fstream>
#include <string.h>

using namespace std;
const char* myPath;

JNIEXPORT jint JNICALL Java_foo_foo(JNIEnv *env, jobject thisobj, jbyteArray passedPath){

/*Some conversion, don't think it is relevant, but it might be*/

    jboolean isCopy;
    jbyte* path = env->GetByteArrayElements(passedPath,&isCopy);
    int length1 = strlen((const char*)path);
    char* convertedVar = (char*)malloc(length1+1);
    memcpy(convertedVar,path,length1);
    convertedVar[length1] = '\0';
    convertedVar = (char*)path;

/*End Conversion*/

    globalVariable = convertedVar;

    ... //Some code to use the variable and releases

}

file2.h(这是我声明全局变量的地方)

#include <vector>
#include <fstream>
#include <string.h>

extern const char* globalVariable;
extern int someNum;
extern int someLength;

class file2{
    public:
        static std::vector<std::string> getSomeString(int &someNum, int &someLength);

    private:
        static const std::vector<std::string> anotherVar;

        ...//some other variables and methods

}

最后是调用 getSomeString 的代码

文件 3.cpp

#include "file2.h"
#include <string.h>
#include <vector>
#include <fstream>

using namespace std;

int someNum = 0;
int someLength = 0;
const char* globalVariable;
vector<string> file2::getSomeString(int &someNum, iint &someLength){

    vector<string> data;

    ifstream infile(globalVariable); //I think the problem is here

    string line;

    ...// some code that goes through the data file specified by the path name    

    someNum = data.size();
    someLength = data[0].size();


return data;
}

const vector<string> file2::anotherVar = getSomeString(someNum,someLength); //variable that uses the getSomeString method

}

我这里再说一遍,我用的是JNI。我收到错误消息说我创建的 lib.so 文件有未定义的变量,我不知道这是否是有用的信息。任何帮助,将不胜感激。对不起,很长的帖子,我很迷茫。

4

2 回答 2

1
char* convertedVar = (char*)malloc(length1+1);
memcpy(convertedVar,path,length1);
convertedVar[length1] = '\0';
convertedVar = (char*)path;

我很确定最后一行不应该在那里。你正在经历这个malloc/memcpy跳舞,然后立即将这个分配和初始化的内存块放在地板上,并convertedVar指出别的东西。

此外,我很确定path一旦函数返回,指针就会变得无效。

于 2013-07-16T20:36:47.503 回答
0

extern const char* globalVariable;

这声明了变量,但没有定义它。您还需要在一个源文件(而不是头文件)中提供定义:

const char* globalVariable;

奇怪的是,你知道的足够用someNumand来做这件事someLength,但不是 for globalVariable

于 2013-07-16T20:10:07.227 回答