[Gc] Question about weak references

Florian Weimer fw at deneb.enyo.de
Mon Dec 25 04:55:40 PST 2006

* Hans Boehm:

> http://developers.sun.com/learning/javaoneonline/2005/coreplatform/TS-32
> 81.pdf

Thanks.  This question is probably totally off-topic, but I ask

I've got an issue with a library that provides two types of objects A
and B, similar to your finalization ordering example.  There are the
following constraints:

  - As references Bs internally.

  - As can be deallocated explicitly (to free up the associated
    storage and locks).

  - The clean-up routine for Bs should be called before program
    termination.  (This is not required for correctness, but it will
    speed up subsequent program invocations.)

  - The B clean-up routine checks that there are any corresponding As
    around and fails in that case.

  - I'd like to offer an interface which permits explicit deallocation
    of Bs, flagging all depending As as unusable.

  - The garbage collector should finalize unused As and Bs, to cover
    up erroneous code paths which do not finalize these objects

All this happens in a Java context.

I had the idea to use arrays of points to As and Bs in native code
which are walked in an atexit() routine.  (And for safety, the
atexit() routine would set a global flag which prevents more
operations on As and Bs.)  The Java objects wrapping sA and Bs would
contain the indexes into those tables.  However, this makes it hard to
address the last point AFAICS.

Is there some cookbook how to tackle such problems?  I'm sure I could
create something that works almost all the time, but there appear to
be so many corner cases to consider that I couldn't be fairly certain
my solution is correct.

More information about the Gc mailing list