Which call should be used instead of LiberateMemory() ?

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
jn0101
Posts: 40
Joined: 2007-06-16T01:36:07-07:00

Which call should be used instead of LiberateMemory() ?

Post by jn0101 »

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
jn0101
Posts: 40
Joined: 2007-06-16T01:36:07-07:00

Re: Which call should be used instead of LiberateMemory() ?

Post by jn0101 »

Update:
I have deleted all calls to LiberateMemory() and code is working now,
although there might be a memory leak.
Post Reply