After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 564890 - lib/persistence.c:167: Memory leak: integer
lib/persistence.c:167: Memory leak: integer
Status: RESOLVED FIXED
Product: dia
Classification: Other
Component: general
devel
Other All
: Normal normal
: 0.97
Assigned To: Dia maintainers
Dia maintainers
Depends on:
Blocks:
 
 
Reported: 2008-12-17 18:31 UTC by aggro
Modified: 2009-01-20 23:29 UTC
See Also:
GNOME target: ---
GNOME version: ---



Description aggro 2008-12-17 18:31:18 UTC
In file lib/persistence.c:167: 
Variable integer leaks memory printf() is executed. It might also leak when insert() is called, depending on how hash table was created. I don't know the code well enough to comment that, but at least with the printf you will leak memory. 


  attr = composite_find_attribute(node, "intvalue");
  if (attr != NULL) {
    integer = g_new(gint, 1);
    *integer = data_int(attribute_first_data(attr));
  } else 
    return;

  if (g_hash_table_lookup(persistent_integers, role) == NULL) 
    g_hash_table_insert(persistent_integers, role, integer);
  else 
    printf("Int %s registered before loading persistence!\n", role);

This bug was found using cppcheck: http://cppcheck.wiki.sourceforge.net/
Comment 1 aggro 2008-12-17 20:24:30 UTC
These two variables have the same problem:

lib/persistence.c:187: Memory leak: realval
lib/persistence.c:207: Memory leak: booleanval
Comment 2 Hans Breuer 2009-01-11 23:23:18 UTC
This is not an unbound leak. It is more a static allocation which gets released by the OS at programs end. (It is similar to every type ever registered with GObject).
Comment 3 aggro 2009-01-12 19:31:11 UTC
You are probably right about GObject part, but I think you missed the point about printf() line. If "Int %s registered before loading persistence!\n" is printed, variable "integer" is not used or released at all.

I think you need something like this (or move the g_hash_table_lookup() call above the g_new() call and don't allocate memory at all if lookup gives value that is not null.

Index: lib/persistence.c
===================================================================
--- lib/persistence.c	(revision 4192)
+++ lib/persistence.c	(working copy)
@@ -162,8 +162,10 @@
 
   if (g_hash_table_lookup(persistent_integers, role) == NULL) 
     g_hash_table_insert(persistent_integers, role, integer);
-  else 
+  else {
     printf("Int %s registered before loading persistence!\n", role);
+    g_free(integer);
+  }
 }
 
 static void
Comment 4 Hans Breuer 2009-01-13 07:42:31 UTC
your are right about what I#ve missed ;) Pathological case but still.
Comment 5 Hans Breuer 2009-01-20 23:29:33 UTC
2009-01-20  Hans Breuer  <hans@breuer.org>

	* lib/persistence.c : use g_warning() instead of printf() for 
	pathological cases, also don't leak memory than; bug #564890.