A user has reported a memory leak in RMagick that occurs in the DeleteMagickRegistry method when RMagick is built with ImageMagick 6.3.1. With 6.3.1, RMagick uses SetMagickRegistry to register an image, then later calls DeleteMagickRegistry to delete the registered image. Here are the relevant calls:
long registry_id;
registry_id = SetMagickRegistry(ImageRegistryType, image, sizeof(Image), &image->exception);
(void) DeleteMagickRegistry(registry_id);
However, DeleteMagickRegistry never actually releases the registered copy of the image. Using gdb, I tracked the call into GetValueFromSplayTree. At line 614, the splay_tree->compare field is NULL, so GetValueFromSplayTree jumps to line 617, where it compares the splay_info->root->key with a pointer to the key, which is registry_id in my code. Since key is just a pointer to a long on the stack, I don't see how this can ever work, so either DeleteMagickRegistry has a bug or I'm using Set/DeleteMagickRegistry incorrectly.
When RMagick is built with 6.3.4 or later, I use the new Set/DeleteImageRegistry methods and there is no memory leak.
RMagick still tries to support 6.0.0 and later, so it would be nice if there was a way to change RMagick to work with Set/DeleteMagickRegistry. Can you recommend a change, or should I ask the user to upgrade to 6.3.4 or later?
Please let me know if you need any more information. Thanks for your help!
Memory leak in DeleteMagickRegistry in 6.3.1
Re: Memory leak in DeleteMagickRegistry in 6.3.1
Delete/SetMagickRegistery() was written very early in the development of ImageMagick and had a few design flaws (such as the memory leak you reported). We implemented Set/DeleteImageRegistry() to fix the design problem and memory leak. Our recommendation is for your RMagick users to upgrade if possible. We could apply a patch to previous versions of ImageMagick but its unlikely the various repositories will adopt the patch and issue updated releases (e.g. Debian, Fedora, Ubuntu, etc).
Re: Memory leak in DeleteMagickRegistry in 6.3.1
I'll recommend an upgrade. Thanks!