Re: [Vala] Help with Memory Leak



On Fri, 2012-07-06 at 14:27 -0400, Bruce Reidenbach wrote:
Hello all,

I am encountering a memory leak in an appindicator menu. The menu is made
up of several RadioMenuItems that have their labels updated once per
second.  I have tracked the memory leak down to the following code, but
cannot seem to be able to make any additional headway.  The method "update"
is called from several sources, and specifies the radio item and the values
to update via a const array of FORMAT statements.  If I comment out the
code that sets the radio menu item label and un-comment the printf
statement, the leak disappears.  The leak is minor -- on the order of about
20 to 30 bytes per invocation -- but over time it adds up.

I'm not sure if this is the correct forum for asking for help, or whether
this is a problem with the gtk+-3.0 or appindicator3-0.1 packages, but I
thought I'd see if anyone has any ideas in the Vala community first.

Thanks,
Bruce

//
-------------------------------------------------------------------------------------
//  Make results more readable by appending the appropriate metric unit to
the data
//
-------------------------------------------------------------------------------------

  private const string suffix [] = { "bytes", "KiB", "MiB", "GiB", "TiB",
"PiB", "EiB", "ZiB" };

  private string readable (float bytes) {
    foreach (string s in suffix) {
      if (bytes < 1024.0) {
        var format = (s == "bytes") ? "%.0f %s" : "%.1f %s";
        return format.printf (bytes, s);
      }
      bytes = bytes / 1024.0f;
    }
    return "%.1f YiB".printf (bytes);
  }

//
-------------------------------------------------------------------------------------
//  Menu update
//
-------------------------------------------------------------------------------------

  public void update (int item, float pct, float val1, float? val2 = 0.0f) {
    var text = (item == 0) ? FORMAT [item].printf (val1)
                           : FORMAT [item].printf (readable (val1),
readable (val2));
    radioItem [item].label = (owned) text;   //  **** Leaky? ****

Get rid of (owned).  That is used to transfer ownership of a reference,
but g_menu_item_set_label doesn't steal the reference you pass it, so
what ends up happening here is that GTK+ will copy the string you pass
it, and since you told Vala that you want to transfer ownership it will
not free the string when it goes out of scope, and you end up leaking
it.


-Evan




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]