So I am trying to wrap Flatbuffers with JNI so I can dynamically return the contents of a buffer as a JSON string using the FBS libs. I have the below code and I keep getting and error of:
1 2 3 4 5 6
JRE version: Java(TM) SE Runtime Environment (8.0_60-b27) (build 1.8.0_60-b27)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.60-b23 mixed mode bsd-amd64 compressed oops)
# Problematic frame:
# C [libc++.1.dylib+0x3df93] _ZNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE7reserveEm+0x1b
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
When I try just doing the NewStringUTF call it never returns which is why I thought trying bytes may help. Any assistance here? I am pretty terrible at c/c++
In the future, when posting code don't omit the function signature and terminating brace. Others shouldn't have to guess the types and names of parameters.
I assume that _text, on line 1, is the parameter that took the value of the 'text' pointer, declared on line 6 in your previous post.
You never initialized that pointer to anything, so it's invalid (it points to nothing). When you call std::string::reserve() on line 3, the function tries to work on a piece of memory that's not an std::string, and the program eventually crashes.
I recommend this rewrite:
1 2 3 4
//std::string* text; (Line 6 in your first post.)
std::string text;
//You'll have to fix the code after this to treat text as an object rather than a pointer.
Since GenerateText() doesn't work if the text pointer is invalid, and you convert it to a reference anyway, you should just make GenerateText() accept a reference:
1 2 3 4 5
return_type GenerateText(/*other arguments*/, std::string &text){
//std::string &text = *_text; You don't need this line anymore.
//...
}
The problem is that the thing I want is the text not the return(which is just rue or false). This is a Flatbuffer lib which I guess I can change but would there be an alternative?
Huh? I don't follow. I just suggested that you change the third parameter of GenerateText(), not the return type.
If you can't change GenerateText() then that's fine. It was just a suggestion. The bug is that the pointer in your first function is uninitialized. That's what you have to change to fix it.
helios - Thanks for the help. I initialized the pointer and all is well now. Given that GenerateText is a 3 party API and is leveraged in way to many other places I decided to leave it along