Which call should be used instead of LiberateMemory() ?
Posted: 2007-06-19T01:49:59-07:00
Hi,
We are having a bug in JMagick concerning setIptcProfile(), setProfileInfo() and setColorProfile().
After these methods are called memory is corrup and the Java virtual machine will crash on the next garbage collection.
However, if we change the code from:
if (profileInfo->info != NULL) {
LiberateMemory((void**) &profileInfo->info);
}
to check if it has a stringlength of 0 to stop it trying to free nothing:
if (profileInfo->info != NULL) {
if (strlen(profileInfo->info))
LiberateMemory((void**) &profileInfo->info);
}
then no crashes occur.
However, this can hardly be the right way to do it.
So, could someone give an example of how to set the profile without
causing memory corruption or leaks ?
Here is the method in its full length (./JMagick/src/magick/jmagick.c )
/*
* Given the C ProfileInfo structure and the Java ProfileInfo object,
* acquire the contents of the Java ProfileInfo object and store it in
* the C ProfileInfo structure.
*
* Input:
* env JNI environment
* profileObj Java ProfileInfo object for which field values are to be
* obtain to store into the C ProfileInfo structure
* Output:
* profileInfo C ProfileINfo structure to store field values
*/
void setProfileInfo(JNIEnv *env, ProfileInfo *profileInfo, jobject profileObj)
{
char *name;
unsigned char *info;
int infoSize = 0;
name = getStringFieldValue(env, profileObj, "name", NULL);
info = getByteArrayFieldValue(env, profileObj, "info", NULL, &infoSize);
if (profileInfo->name != NULL) {
LiberateMemory((void**) &profileInfo->name);
}
profileInfo->name = name;
if (profileInfo->info != NULL) {
LiberateMemory((void**) &profileInfo->info);
}
profileInfo->info = info;
profileInfo->length = infoSize;
}
Thank you for your time.
Jacob
We are having a bug in JMagick concerning setIptcProfile(), setProfileInfo() and setColorProfile().
After these methods are called memory is corrup and the Java virtual machine will crash on the next garbage collection.
However, if we change the code from:
if (profileInfo->info != NULL) {
LiberateMemory((void**) &profileInfo->info);
}
to check if it has a stringlength of 0 to stop it trying to free nothing:
if (profileInfo->info != NULL) {
if (strlen(profileInfo->info))
LiberateMemory((void**) &profileInfo->info);
}
then no crashes occur.
However, this can hardly be the right way to do it.
So, could someone give an example of how to set the profile without
causing memory corruption or leaks ?
Here is the method in its full length (./JMagick/src/magick/jmagick.c )
/*
* Given the C ProfileInfo structure and the Java ProfileInfo object,
* acquire the contents of the Java ProfileInfo object and store it in
* the C ProfileInfo structure.
*
* Input:
* env JNI environment
* profileObj Java ProfileInfo object for which field values are to be
* obtain to store into the C ProfileInfo structure
* Output:
* profileInfo C ProfileINfo structure to store field values
*/
void setProfileInfo(JNIEnv *env, ProfileInfo *profileInfo, jobject profileObj)
{
char *name;
unsigned char *info;
int infoSize = 0;
name = getStringFieldValue(env, profileObj, "name", NULL);
info = getByteArrayFieldValue(env, profileObj, "info", NULL, &infoSize);
if (profileInfo->name != NULL) {
LiberateMemory((void**) &profileInfo->name);
}
profileInfo->name = name;
if (profileInfo->info != NULL) {
LiberateMemory((void**) &profileInfo->info);
}
profileInfo->info = info;
profileInfo->length = infoSize;
}
Thank you for your time.
Jacob