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 764692 - Minor memory leaks in champlain-map-source-factory and champlain-view
Minor memory leaks in champlain-map-source-factory and champlain-view
Status: RESOLVED FIXED
Product: libchamplain
Classification: Core
Component: General
unspecified
Other Linux
: Normal normal
: ---
Assigned To: libchamplain-maint
libchamplain-maint
Depends on:
Blocks:
 
 
Reported: 2016-04-06 16:32 UTC by Marius Stanciu
Modified: 2016-04-10 10:37 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
champlain-map-source-factory: Fix memory leak in champlain_map_source_new_generic() (901 bytes, patch)
2016-04-06 16:36 UTC, Marius Stanciu
none Details | Review
champlain-view: Fix memory leak in tile_map_set (886 bytes, patch)
2016-04-06 16:42 UTC, Marius Stanciu
committed Details | Review
Code snippet describing memory leak in tile_map_set (119.78 KB, image/png)
2016-04-09 15:37 UTC, Marius Stanciu
  Details
champlain-map-source-factory: Fix memory leak in map source constructors (3.16 KB, patch)
2016-04-09 18:50 UTC, Marius Stanciu
none Details | Review

Description Marius Stanciu 2016-04-06 16:32:10 UTC
Memory leaks and fixes:

1) g_object_get() creates copies which are never destroyed.
https://developer.gnome.org/gobject/unstable/gobject-The-Base-Object-Type.html#g-object-get

2) g_hash_table_remove() destroys the key in the hash table(if found) but not the one sent as parameter for comparison
https://developer.gnome.org/glib/2.34/glib-Hash-Tables.html#g-hash-table-remove
Comment 1 Marius Stanciu 2016-04-06 16:36:16 UTC
Created attachment 325495 [details] [review]
champlain-map-source-factory: Fix memory leak in champlain_map_source_new_generic()
Comment 2 Marius Stanciu 2016-04-06 16:42:54 UTC
Created attachment 325496 [details] [review]
champlain-view: Fix memory leak in tile_map_set
Comment 3 Nayan Deshmukh 2016-04-06 18:26:12 UTC
(In reply to Marius Stanciu from comment #1)
> Created attachment 325495 [details] [review] [review]
> champlain-map-source-factory: Fix memory leak in
> champlain_map_source_new_generic()

The variables that you freed are supplied to champlain_network_tile_source_new_full() which then creates a new source with g_object_new(). The g_object_new() uses this variables so freeing them might create problem.
Comment 4 Marius Stanciu 2016-04-06 19:47:41 UTC
(In reply to Nayan Deshmukh from comment #3)
> (In reply to Marius Stanciu from comment #1)
> > Created attachment 325495 [details] [review] [review] [review]
> > champlain-map-source-factory: Fix memory leak in
> > champlain_map_source_new_generic()
> 
> The variables that you freed are supplied to
> champlain_network_tile_source_new_full() which then creates a new source
> with g_object_new(). The g_object_new() uses this variables so freeing them
> might create problem.

I'm not entirely sure about this, but from what I tested, g_object_new makes copies of const ghar* value parameters. That means the original gchar array remains unfreed.
Comment 5 Jiri Techet 2016-04-08 21:46:15 UTC
Marius, good catch with the champlain_map_source_new_generic(), this really looks like a leak and your patch looks correct to me. I'm just wondering if it wouldn't be better to call the getters from ChamplainMapSourceDesc instead of g_object_get() - the getters don't do any reffing so the unrefs and frees wouldn't be necessary. Also something similar should be done for champlain_map_source_new_memphis().

By the way, it's not the g_object_get() which makes the reference - for instance with ChamplainMapSourceDesc it causes that the corresponding property gets retrieved by champlain_map_source_desc_get_property() and then it's the call of e.g. g_value_set_string() which duplicates the string.

Regarding the second patch with the hash table, this doesn't look right (unless I miss something). The hash table is created using

  priv->tile_map = g_hash_table_new_full (g_int64_hash, g_int64_equal, slice_free_gint64, NULL);

which means that when the key is removed it's always freed with slice_free_gint64() and no extra free is needed.
Comment 6 Marius Stanciu 2016-04-09 15:35:03 UTC
(In reply to Jiri Techet from comment #5)
> Marius, good catch with the champlain_map_source_new_generic(), this really
> looks like a leak and your patch looks correct to me. I'm just wondering if
> it wouldn't be better to call the getters from ChamplainMapSourceDesc
> instead of g_object_get() - the getters don't do any reffing so the unrefs
> and frees wouldn't be necessary. Also something similar should be done for
> champlain_map_source_new_memphis().
> 
> By the way, it's not the g_object_get() which makes the reference - for
> instance with ChamplainMapSourceDesc it causes that the corresponding
> property gets retrieved by champlain_map_source_desc_get_property() and then
> it's the call of e.g. g_value_set_string() which duplicates the string.
> 
The g_object_get() and all the memory management that comes after sure looks unnecessary in this case. I'll attach a patch using the class getters.

> Regarding the second patch with the hash table, this doesn't look right
> (unless I miss something). The hash table is created using
> 
>   priv->tile_map = g_hash_table_new_full (g_int64_hash, g_int64_equal,
> slice_free_gint64, NULL);
> 
> which means that when the key is removed it's always freed with
> slice_free_gint64() and no extra free is needed.

I've tested it both manually and with valgrind, and it definitely is a major leak(hundred of unfreed variables occur when panning the map)

Here, I've cropped up a small code snippet to illustrate exactly what's going on as it is easy to miss. (see attached screenshot)

The exact same thing is happening in tile_map_set()
Comment 7 Marius Stanciu 2016-04-09 15:37:10 UTC
Created attachment 325639 [details]
Code snippet describing memory leak in tile_map_set
Comment 8 Jiri Techet 2016-04-09 17:37:53 UTC
Review of attachment 325496 [details] [review]:

Sorry, I misunderstood what you said - I didn't notice the remove case. Merged now, thanks.
Comment 9 Marius Stanciu 2016-04-09 18:50:20 UTC
Created attachment 325642 [details] [review]
champlain-map-source-factory: Fix memory leak in map source constructors
Comment 10 Jiri Techet 2016-04-10 10:37:59 UTC
Thanks, committed.