Bug#369083: Memory leak in gnome-cups-icon comes from libgnomecups

Samuel Mimram samuel.mimram at ens-lyon.org
Mon Jul 3 17:08:07 UTC 2006


reassign 369083 libgnomecups1.0-1
tag 369083 + patch
thanks

Hi,

I think I found out why gnome-cups-icon was taking so much memory over
the time. After a few minutes of valgrinding, the main memory leak was
obviously this one:

900,877 bytes in 52,994 blocks are definitely lost in loss record 151 of 151
   at 0x4A1B80D: malloc (vg_replace_malloc.c:149)
   by 0x8933211: strdup (in /lib/libc-2.3.6.so)
   by 0x7A5E50A: gnome_cups_request_add_requested_attributes
(gnome-cups-request.c:482)
   by 0x7A59B51: update_attributes (gnome-cups-printer.c:321)
   by 0x7A5C7A2: update_printers (gnome-cups-printer.c:728)
   by 0x7A5C7F1: update_printers_timeout (gnome-cups-printer.c:757)
   by 0x86247DA: (within /usr/lib/libglib-2.0.so.0.1000.3)
   by 0x8624148: g_main_context_dispatch (in
/usr/lib/libglib-2.0.so.0.1000.3)
   by 0x86272B4: (within /usr/lib/libglib-2.0.so.0.1000.3)
   by 0x86275B5: g_main_loop_run (in /usr/lib/libglib-2.0.so.0.1000.3)
   by 0x5AD2251: gtk_main (in /usr/lib/libgtk-x11-2.0.so.0.800.18)
   by 0x4049C8: main (gnome-cups-icon.c:136)

So I had a look at the source of the function
gnome_cups_request_add_requested_attributes in libgnomecups:

void
gnome_cups_request_add_requested_attributes (ipp_t *request,
					     ipp_tag_t group,
					     int n_attributes,
					     char **attributes)
{
	ipp_attribute_t *attr;
	int i;
	
	attr = ippAddStrings (request,
			      group,
			      IPP_TAG_KEYWORD,
			      "requested-attributes",
			      n_attributes, NULL, NULL);

	for (i = 0; i < n_attributes; i++) {
	attr->values[i].string.text = gnome_cups_strdup(attributes[i]);
	}
}

The problem here is that gnome_cups_strdup (which is the same as strdup)
allocates memory which is never freed by ippDelete. Namely, cups seems
to use its own memory management, by reference counting AFAICT, and the
strdupped strings are not taken in account. The fix is simple here: when
its last argument is not NULL and the tag IPP_TAG_COPY is present, the
function ippAddStrings does the copy of the strings on its own and
registers them in CUPS' memory management thing, as you can see in the
source of ippAddString:

value->string.text = ((int)type & IPP_TAG_COPY) ? (char *)values[i] :
_cupsStrAlloc(values[i]);

The attached patch seems to solve the memory leak.

Thanks!

Cheers,

Samuel.

PS: Jody, you can find the full debian bug report here:
http://bugs.debian.org/369083

PPS: maybe, some day, the world will understand that memory management
shouldn't be done by hand...
-------------- next part --------------
--- libgnomecups-0.2.2.orig/libgnomecups/gnome-cups-request.c
+++ libgnomecups-0.2.2/libgnomecups/gnome-cups-request.c
@@ -469,18 +469,14 @@
 					     int n_attributes,
 					     char **attributes)
 {
-	ipp_attribute_t *attr;
-	int i;
 	
-	attr = ippAddStrings (request, 
-			      group,
-			      IPP_TAG_KEYWORD,
-			      "requested-attributes",
-			      n_attributes, NULL, NULL);
-
-	for (i = 0; i < n_attributes; i++) {
-		attr->values[i].string.text = gnome_cups_strdup (attributes[i]);
-	}
+	ippAddStrings (request,
+		      group,
+		      IPP_TAG_KEYWORD & IPP_TAG_COPY,
+		      "requested-attributes",
+		      n_attributes,
+		      NULL,
+		      (const char**)attributes);
 }
 
 typedef struct


More information about the Pkg-gnome-maintainers mailing list