Gee Destructors in LinkedList



Hello everyone,

Consider this situation:

using Gee;

public class Bar: Object {
  ~Bar() {
    stdout.printf("Bar destruct.\n");
  }

}

public class Foo: Object {
  public Gee.LinkedList<Bar> bars { get; private set; }

  public Foo() {
    this.bars = new Gee.LinkedList<Bar> ();
  }

  ~Foo() {
    stdout.printf("Foo destruct: \n");

    foreach (Bar bar in this.bars) {
      stdout.printf("\t%u\n", bar.ref_count); // ref_count will be +1
in this block because of the local bar reference
    }

    stdout.printf("\n");
  }
}

public static int main() {
  Foo foo = new Foo();

  return 0;
}

Observe that when the LinkedList has one item, then that one item's
~Bar gets called (meaning correct memory management). But if the list
has more than one item, then ~Bar doesn't get called for any item.
This to me clearly demonstrates a memory management problem, or a
forgotten unref() at ~LinkedList in Gee. This can further be tested by
manually unreffing bar in the foreach loop at ~Foo. If in the
LinkedList only one item is present, then GLib complains of a
premature unref, however if there are more than one items in the
LinkedList, and a manual unref is issued, GLib does not complain of a
premature unref and as it should each element's destructor gets
called.

I'm using Vala 0.9.1 to test this and Gee 0.5.1 on Ubuntu Lucid 32 bit.


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