[concurrency-interest] Object finalization

Bob Lee crazybob at crazybob.org
Sun May 13 16:45:43 EDT 2012


The object is considered "reachable" while the constructor is executing (JLS
12.6.1<http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.6.1>),
and 'a' will never be set to null by the GC, so this code is safe if
assertions are enabled (they're off by default). :-)

Bob
Square is hiring! <https://squareup.com/jobs>

On Sun, May 13, 2012 at 1:22 PM, Chris Vest <mr.chrisvest at gmail.com> wrote:

> Hi,
> I could not find any resources that I felt were clear and trustworthy
> enough, so...
>
> I would like to have confirmed (or busted) my assumption that the
> following code is buggy, because the garbage collector might set the `a`
> field to null, before this `finalize` method can read it, thus preventing
> the method from attempting to cleanly dispose of the `Resource` in the `b`
> field — this all under the assertion that `safelyDispose` itself never
> throws:
>
> public class SomeService {
>   private final Resource a, b;
>
>   public SomeService(Resource a, Resource b) {
>     assert a != null && b != null;
>     this.a = a;
>     this.b = b;
>   }
>   @Override
>   protected void finalize() throws Throwable {
>     a.safelyDispose();
>     b.safelyDispose();
>   }
> }
>
> Cheers,
> Chris
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20120513/187a1fe1/attachment-0001.html>


More information about the Concurrency-interest mailing list