Re: Breaking reference cycles ( continued from ages ago )




On Dec 15, 2005, at 6:32 PM, Daniel Kasak wrote:

However in Gtk2::Ex::Datasheet::DBI, this doesn't work. This could very well be because I'm using some custom cell renderers that set up their own signal handlers, eg:

$editable -> signal_connect(key_press_event => sub { ... some stuff ...});

Do I have to clean this stuff up as well? The cell renderers aren't set up in an OO way ( ie they can't see $self that the rest of Gtk2::Ex::Datasheet::DBI sees ). This will make my above approach of stuffing them in an array and disconnecting them later more complicated.

Not quite sure i follow exactly where your cycles are occurring, but there are a few different things you can do:

- Try to avoid using an external reference to an instance in a closure; use the signal handler's arguments as much as possible. E.g.,

# avoid this -- it traps the external $button in the closure for the
      # signal handler, making $button refer to itself.
      $button->signal_connect (clicked => sub {
               $button->foo();
      });

      # do this instead:
      $button->signal_connect (clicked => sub {
              my $button = (@_);
              $button->foo();
      });


- GtkObject's destroy signal exists so that you can break reference cycles explicitly; try breaking relevant links there.

    $widget_with_lots_of_links->signal_connect (destroy => sub {
        my $self = shift;
        delete $self->{other_widget_1};
        delete $self->{other_widget_2};
        delete $self->{tree_model};
        # usually there's only one or two key things to break here.
    });


- Many reference cycles occur because object A owns object B, but object B has to have a backreference to A in order to update A on some change in B. This is a perfect reason to create a new signal and decouple B from A. Not only does it clean up some of your reference cycles, the reduced coupling makes the program a bit easier to maintain.


--
I don't like... this game... when there's cannons... being shot... at me.
  -- Elysse




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