"Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org> wrote in message
Mike Schilling wrote:
OK. Can you give me an example of how you'd use a PhantomReference
programmatically? I've asked this before on the n.g, and never gotten an
answer.
I'd say that practically every application where you either don't want to,
don't need to, or don't want /to/ need to, make the object itself know
about,
or carry knowledge needed for, the cleanup.
I.e. when you want to track object's lifetimes for their effect on some
/other/
object, but have (otherwise) no interest in the objects themselves.
For instance, say you allocate objects on behalf of some group of
customers.
You want (for whatever arcane reason) to track how many objects are still
(roughly) alive for each customer. This is an accounting-like tracking,
and
the objects themselves have no interest in either the tracking or in which
customer they are active on behalf of. So you use a phantom reference for
each
object, which also contains (by subclassing) a reference to the customer.
Thus
you can do the accounting for each customer without messing around with
the
objects themselves, and do it (I presume, but don't know for sure) more
efficiently and with less chance of awkward interactions with finalisation
than
if you'd used the same architecture but with weak refs.
provided that in both cases you just queue the ref and forget about it. In
but you have to remember to clear the phantom ref yourself.