Improve test for garbage collection of trait class with listeners.

I used to rely on the length of `gc.get_objects()` as the invariant for
number of objects before/after the trait class instance is created and
destroyed. That turns out to be unreliable (the internal state of the
interpreter can influence that).

Instead, we're using a weak reference callback to listen to the object
dying.