Closed codeservice closed 3 years ago
Yes, that code is wrong. the std::string will deallocate when it leaves scope and mess-up the return pointer (same goes for the empty pointer). You could either return the std::string by value, and then take the c_str in the calling function, or do something like this (assuming you are attached to the GC)
HxString JStringToHxString(JNIEnv *env, jstring inString, bool inReleaseLocalRef)
{
const char *c_ptr = env->GetStringUTFChars(inString, 0);
HxString result(c_ptr);
env->ReleaseStringUTFChars(inString, c_ptr);
if (inReleaseLocalRef)
env->DeleteLocalRef(inString);
return result;
}
It is also safe to take the c_str in this case because the hxcpp GC should be able to identify it on the stack. But if you just want a "const char *" for c++ code, you could use:
...
if (mid == 0)
return "";
jstring jPref = (jstring) env->CallStaticObjectMethod(cls, mid);
const char *c_ptr = env->GetStringUTFChars(jPref, 0);
int len = strlen(c_ptr);
const char *result = alloc_string_data(c_ptr, len);
env->ReleaseStringUTFChars(jPref, c_ptr);
return result;
which avoid possible translations between wide-char encoding
I tested keep using "const char*" option. It works great and require minimal code changes. To save time I will propose this change to github ...
After upgrading to latest hxcpp and nme I can see strange issue on android x86_64 arc so far and can't see this on windows. Text looks currupt after using GetClipboardText from System.cspp:
const char GetClipboardText(){ JNIEnv env = GetEnv(); jclass cls = FindClass("org/haxe/nme/GameActivity"); jmethodID mid = env->GetStaticMethodID(cls, "getClipboardText", "()Ljava/lang/String;"); if (mid == 0) return std::string("").c_str();
I was able to fix it by using:
HxString GetClipboardText(){ instead of const char* GetClipboardText(){
In hxcpp I can see only difference -O2 flag and in NME JNI interface was updated. Not sure what change exactly break cliboard on android.
Also maybe instead of HxString make sence to use something like std::string CapabilitiesGetLanguage() {