Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » [Databinding] observable disposal best practices
[Databinding] observable disposal best practices [message #493561] Mon, 26 October 2009 22:18 Go to next message
Will Horn is currently offline Will HornFriend
Messages: 265
Registered: July 2009
Senior Member
Is there a place for documentation about observable disposal? For example,
in the platform help, there are some rules of thumb for SWT resource
disposal (copied below).

A few things I've learned:

* Observables from SWTObservables get disposed automatically with the
control being observed
* Detail observables from MasterDetailObservables get disposed with the
master
* DataBindingContext#dispose does not dispose bound observables, but it does
dispose ValidationStatusProviders
* WritableList, WritableValue need to be disposed manually
* BeansObservables and PojoObservables need to be disposed manually

I'm sure one of the framework authors could make this more precise/complete,
but I'm happy to contribute/work on it. It would also be good to have a
discussion about why/when disposal is important. For example, in some
cases, it seems unnecessary, e.g. if I bind a WritableValue to a Text widget
then dispose the DataBindingContext, it's seems unnecessary to dispose the
WritableValue - even though it's not disposed it can still be garbage
collected since the only listener was removed.

-Will

----------------

*SWT Resource disposal* (from
http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse .platform.doc.isv/guide/swt_widgets.htm )

The platforms underneath SWT require explicit allocation and freeing of OS
resources. In keeping with the SWT design philosophy of reflecting the
platform application structure in the widget toolkit, SWT requires that you
explicitly free any OS resources that you have allocated. In SWT, the
Widget.dispose() method is used to free resources associated with a
particular toolkit object.

The rule of thumb is that if you create the object, you must dispose of it.
Here are some specific ground rules that further explain this philosophy:

* If you create a graphic object or widget using a constructor, you must
explicitely dispose of it when you are finished using it.
* When a Composite, is disposed, the composite and all of its child widgets
are recursively disposed. In this case, you do not need to dispose of the
widgets themselves. However, you must free any graphics resources allocated
in conjunction with those widgets.
* If you get a graphic object or widget without using a constructor (e.g.
Control.getBackground()), do not dispose of it since you did not allocate
it.
* If you pass a reference to your widget or graphic object to another
object, you must take care not to dispose of it while it is still being
used. (Similar to the rule described in Plug-in patterns for using images.)
* If you create a graphic object for use during the lifetime of one of your
widgets, you must dispose of the graphic object when the widget is disposed.
This can be done by registering a dispose listener for your widget and
freeing the graphic object when the dispose event is received.

There is one exception to these rules. Simple data objects, such as
Rectangle and Point, do not use operating system resources. They do not have
a dispose() method and you do not have to free them. If in doubt, check the
javadoc for a particular class.

See Managing operating resources for further discussion of this topic.
Re: [Databinding] observable disposal best practices [message #493663 is a reply to message #493561] Tue, 27 October 2009 14:00 Go to previous messageGo to next message
Matthew Hall is currently offline Matthew HallFriend
Messages: 368
Registered: July 2009
Senior Member
Will Horn wrote:
> A few things I've learned:
>
> * Observables from SWTObservables get disposed automatically with the
> control being observed
> * Detail observables from MasterDetailObservables get disposed with the
> master
> * DataBindingContext#dispose does not dispose bound observables, but it
> does dispose ValidationStatusProviders
> * WritableList, WritableValue need to be disposed manually
> * BeansObservables and PojoObservables need to be disposed manually

These are good facts to document for users.

In general you have to dispose observables yourself, although in many
cases they may be disposed for you, when the object upon which the
observable depends is disposed.

> I'm sure one of the framework authors could make this more
> precise/complete, but I'm happy to contribute/work on it. It would also
> be good to have a discussion about why/when disposal is important. For
> example, in some cases, it seems unnecessary, e.g. if I bind a
> WritableValue to a Text widget then dispose the DataBindingContext, it's
> seems unnecessary to dispose the WritableValue - even though it's not
> disposed it can still be garbage collected since the only listener was
> removed.

Perhaps, but you should dispose it anyway since there might be detail
observables which use that WritableValue as a master, and they would
still have listeners attached.

> * If you create a graphic object or widget using a constructor, you must
> explicitely dispose of it when you are finished using it.

This would be a good rule for observables too.

> * When a Composite, is disposed, the composite and all of its child
> widgets are recursively disposed. In this case, you do not need to
> dispose of the widgets themselves. However, you must free any graphics
> resources allocated in conjunction with those widgets.

This translates to DataBinding in terms of observable-dependency
relationships. As you already noted, master-detail observables are
automatically disposed with the master observable. This is also the
case with widget/viewer observables when the underlying widget is disposed.

> * If you get a graphic object or widget without using a constructor
> (e.g. Control.getBackground()), do not dispose of it since you did not
> allocate it.

Same applies to observables you did not directly instantiate, e.g.
ValidationStatus.getValidationStatus() since this should be performed by
the ValidationStatus object upon disposal.

All of these things would be really great to have documented probably in
the IObservable.dispose() javadoc as well as on the wiki. Would you
mind filing a bug for this?

Matthew
Re: [Databinding] observable disposal best practices [message #493665 is a reply to message #493663] Tue, 27 October 2009 14:10 Go to previous messageGo to next message
Matthew Hall is currently offline Matthew HallFriend
Messages: 368
Registered: July 2009
Senior Member
I forgot to mention that ObservablesManager is a good helper class for
tracking dependencies and cleaning up after yourself.

DataBindingContext ctx = new DataBindingContext();
ObservablesManager om = new ObservablesManager();
om.runAndCollect(new Runnable() {
public void run() {
// Create all observables and bindings in here.
// Using ObservableTracker magic, ObservablesManager will know about
// every observable created within this run method.
}
});
om.addObservablesFromContext(ctx, true, true);

// Later when it is time to cleanup..
om.dispose();
ctx.dispose();

One note: make sure you dispose the ObservablesManager *before* the
DataBindingContext, since ObservablesManager relies on the
DataBindingContext having all its binding there in order to know which
observables are to be disposed. If you dispose the DataBindingContext
first then ObservablesManager can no longer discover those observables
and thus cannot dispose them.

Matthew
Re: [Databinding] observable disposal best practices [message #493745 is a reply to message #493663] Tue, 27 October 2009 19:15 Go to previous messageGo to next message
Will Horn is currently offline Will HornFriend
Messages: 265
Registered: July 2009
Senior Member
"Matthew Hall" <matthall@woodcraftmill.com> wrote in message
news:hc6ud4$1bk$1@build.eclipse.org...
> All of these things would be really great to have documented probably in
> the IObservable.dispose() javadoc as well as on the wiki. Would you mind
> filing a bug for this?
http://bugs.eclipse.org/293492
Re: [Databinding] observable disposal best practices [message #494518 is a reply to message #493665] Sat, 31 October 2009 00:53 Go to previous message
Allen D. McDonald is currently offline Allen D. McDonaldFriend
Messages: 13
Registered: July 2009
Junior Member
Matthew, Is there a snippet that uses this?
Previous Topic:[Databinding] use for SWTObservables.observe(Text)
Next Topic:TableViewer with custom tooltips and styling
Goto Forum:
  


Current Time: Thu Apr 25 04:23:18 GMT 2024

Powered by FUDForum. Page generated in 0.03454 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top