Search code examples
c++java-native-interfacevariable-assignment

How can you assign a variable a value from another class?


I am writing a program that needs a pathname to eventually create a global string. I currently have this pathname hard coded, but would like to use a global variable to replace this. The problem I am having is that my global variable is undefined. I am also using JNI and the error I am getting is:

#
# 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.
#

and here is relevant code that is reformatted for simplicity:

file1.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 (This is where I declare the global variable)

#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

}

and finally the code that calls getSomeString

file3.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

}

I'll say it here again, I am using JNI. I have gotten errors saying the lib.so file I create has undefined variables, I don't know if this is helpful information. Any help would be appreciated. Sorry about the long post, I am quite lost.


Solution

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

    I'm pretty sure this last line shouldn't be there. You are going through this malloc/memcpy dance, then promptly drop this allocated-and-initialized block of memory on the floor, and make convertedVar point to something else instead.

    Further, I'm pretty sure path pointer will become invalid as soon as the function returns.