shoes/shoes4 — pull request #655 by wasnotrice (2014-04-19 06:57:56+00:00)
Title: Experiment with disposing SWT resources

Here's some initial work on #649. I thought it was worth addressing, since it might be able to solve #596.

I started with TextBlock, since it seems like the manual has lots of those, and they might be causing trouble. The objects we need to dispose there are `::Swt::Color` and `::Swt::Font` objects. It's tricky to know when to dispose them, though, because in the case of the `FittedTextLayout`, those objects get created in factories, then passed to `Swt::TextLayout` objects. Those layout objects do *not* need to be disposed, because they don't hold native resources (I know this the hard way).

I have tried two strategies here. Please review.

1. Dispose the `Font` created in the `TextBlock` object after it gets painted by the `TextBlockPainter`. (7a17194)
2. Dispose *all* `Swt::Color` objects (these are the `@real` objects) when their `Shoes::Swt::Color` parent is garbage collected. (1069008)

Strategy 1 is doable in some cases, but mabye not in all. Strategy 2 seems potentially useful, but also easy to screw up. See [this blog post](http://www.mikeperham.com/2010/02/24/the-trouble-with-ruby-finalizers/). I have added a line that logs every object disposed this way, and a script to track them. Here's a sample run:

```
$ bin/track_dispose samples/simple-manual.rb
Logging SWT objects disposed in finalizers to dispose_log.txt
Running `bin/shoes samples/simple-manual.rb > dispose_log.txt`...quit to see results
2014-04-19 01:40:02.051 java[18210:d0f] *** WARNING: Method userSpaceScaleFactor in class NSWindow is deprecated on 10.7 and later. It should not be used in new applications. Use convertRectToBacking: instead.

Started at 01:39:56
Finished at 01:40:17
Elapsed: 20.909 sec

1479 objects disposed in finalizers (70.73509015256589 disposals/sec)
```

You can see how we might run out of handles, and remember, this is only tracking `Color`.