How can I dispose of an object (say a Bitmap) when it becomes orphaned ?

Posted by Jelly Amma on Stack Overflow See other posts from Stack Overflow or by Jelly Amma
Published on 2010-03-26T05:29:40Z Indexed on 2010/03/26 5:33 UTC
Read the original article Hit count: 266

I have a class A providing Bitmaps to other classes B, C, etc.

Now class A holds its bitmaps in a ring queue so after a while it will lose reference to the bitmap.

While it's still in the queue, the same Bitmap can be checked out by several classes so that, say, B and C can both hold a reference to this same Bitmap. But it can also happen that only one of them checked out the Bitmap or even none of them.

I would like to dispose of the bitmap when it's not being needed any more by either A, B or C.

I suppose I have to make B and C responsible for somehow signaling when they're finished using it but I'm not sure about the overall logic.

Should it be a call to something like DisposeIfNowOrphan() that would be called :

1 - when the Bitmap gets kicked out of the queue in class A

2 - when B is finished with it

3 - when C is finished with it

If that's the best strategy, how can I evaluate the orphan state ?

Any advice would be most welcome.

© Stack Overflow or respective owner

Related posts about dispose

Related posts about sharedobjects