NDK Support for Android 4.4 and below, or a bug?

steviegiovanni
edited May 2015 in Native
Hi Photon Team,

I'm sending a string through the network (I'm using the loadbalancing sample server) using the following lines
ExitGames::Common::Hashtable evData;
evData.put(KEY_FIELDNAME,(ExitGames::Common::JString(reinterpret_cast<char*>(fieldname))));

On the other end, I'm receiving this string and using the content as an input to my other function
ExitGames::Common::JString fieldj = ((ExitGames::Common::ValueObject<ExitGames::Common::JString>)(evData.getValue(KEY_RPCFIELD))).getDataCopy();
char *fieldname = new char[fieldj.length()+1];
wcstombs(fieldname, fieldj.cstr(), fieldj.length()+1);

...
otherfunction(fieldname)
...

delete [] fieldname;
fieldname = NULL;

I'm testing my application on 5 different devices
- Desktop (windows)
- Motorolla Nexus 6 (Android 5.1)
- Samsung Galaxy Tab S(Android 5.0)
- Samsung Galaxy Tab 3 (Android 4.4.2)
- Asus Nexus 7 (Android 4.4.3)

For the first 3 devices, my code works. The fieldname string is transferred successfully and I can use it in my function. But for the devices that run Android 4.4.x, it seems like my string is altered, thus my function, which uses the string as an input, causes a crash. Have you guys experienced similar problems or is this a known issue? Thanks in advance!

-Stevie

Comments

  • Hi Stevie.

    The line which is causing your issues is wcstombs(fieldname, fieldj.cstr(), fieldj.length()+1);.
    So this is not a bug in Photon, but in Android itself.
    The Android versions of stlport and gnustl both do not offer a working implementation of wcstombs(). It works correctly when specifying c++_static (clang stl) as the standard library of choice.
    For reference please visit http://stackoverflow.com/questions/2545 ... on-android.

    However you don't have to use wcstombs() at all, but could use Photons integrated string conversion facilities, which work fine even with Android 2.2.

    Instead of
    [code2=cpp]char *fieldname = new char[fieldj.length()+1];
    wcstombs(fieldname, fieldj.cstr(), fieldj.length()+1);

    ...
    otherfunction(fieldname)
    ...

    delete [] fieldname;
    fieldname = NULL;[/code2]

    you could just write
    [code2=cpp]ExitGames::Common::UTF8String fieldname = fieldj.UTF8Representation();
    otherfunction(fieldname.cstr())[/code2]
  • Hi Kaiserludi,

    Thanks! I will try it out.

    -Stevie