Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ParameterString memory management bug
06-09-2011, 08:15 PM
Post: #1
ParameterString memory management bug
The JNI method Java_org_openhome_net_core_ParameterString_ServiceParameterCreateString has a memory management bug. It calls ReleaseStringUTFChars() on the elements of the allowed[] array too early, before ohNet has finished using the strings. This causes messages like the following:

Validation of Parameter BrowseFlag failed, asserting
Assertion failed. OpenHome/Net/ControlPoint/CpiService.cpp:121

because the validation array has been overwritten by the time the validation code in ParameterString::ValidateString runs.

I fixed it by changing this code:
Code:
for (i = 0; i < aCount; i++)
{
    jstring allowedString = (*aEnv)->GetObjectArrayElement(aEnv, aAllowedValues, i);
    const char* nativeAllowedString = (*aEnv)->GetStringUTFChars(aEnv, allowedString, NULL);
    allowed[i] = (char *) nativeAllowedString;
    (*aEnv)->ReleaseStringUTFChars(aEnv, allowedString, allowed[i]);
}

param = ServiceParameterCreateString(name, allowed, aCount);
to the following:
Code:
for (i = 0; i < aCount; i++)
{
    jstring allowedString = (*aEnv)->GetObjectArrayElement(aEnv, aAllowedValues, i);
    const char* nativeAllowedString = (*aEnv)->GetStringUTFChars(aEnv, allowedString, NULL);
    allowed[i] = (char *) nativeAllowedString;
}

param = ServiceParameterCreateString(name, allowed, aCount);
for (i = 0; i < aCount; i++)
{
    jstring allowedString = (*aEnv)->GetObjectArrayElement(aEnv, aAllowedValues, i);
    (*aEnv)->ReleaseStringUTFChars(aEnv, allowedString, allowed[i]);
}
It's not very pretty. but it seems to work!
Find all posts by this user
07-09-2011, 08:38 AM
Post: #2
RE: ParameterString memory management bug
Thank you for reporting this.

This has been passing in the tests, but it was obviously just through a lucky coincidence that the memory hadn't yet been re-allocated!

Sadly, I don't know of a nicer way to allocate and then free collections of native strings in JNI, other than using the two loops you suggested. However, string parameters usually have few allowed values, so the performance penalty shouldn't be too great.

Your fix is in the internal repository and should make it out to GitHub within a couple of days.
Find all posts by this user


Forum Jump: