Re: [Vala] Help with Memory Leak



On 07/06/2012 08:27 PM, 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.

[snip]

-------------------------------------------------------------------------------------
//  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? ****
  //stdout.printf (text + "\n");

    var ptr = (int) (pct * 10.0f);
    if (selected == item) appIcon.set_icon_full (icons [ptr], NAME);
  }

I don't claim to actually understand the object model, but I have an idea:

It stands to reason that the label string is not properly freed. It
looks like, though ownership is transferred to radioItem, it does not
know about this, and no code to free the string is around.

Try adding a line

string old_text = (owned) radioItem [item].label;

This should have Vala take care of it?

Thomas



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