[ImageJ-devel] when to close cursors, was Re: [fiji-devel] imglib coding style

Albert Cardona sapristi at gmail.com
Fri Apr 16 15:25:56 CDT 2010


A short note: the cursor class could have an @Override on the
finalize() method from class Object, and in it call .close().

An object's finalize() method is called when garbage-collected. IIRC,
when an object created within a method is no longer referenced (i.e.
the method returned), its finalize() method is called.

If calling close() twice doesn't hurt, then adding it in there would
prevent future errors by programmers new to imglib (and provide a bit
of a safety net).

Of course documenting the need of calling .close() makes sense.

The standard way of iterating an image from imglib with a Cursor should be:

final Cursor c;

try {
    c = ....
    while (c.hasNext()) {
        ...
    }
} finally {
    c.close();
}


In the upcoming Java 7, there's "Automatic Resource Management", which
is nothing other than an automatic call to "close()" on a Reader
interface (and hopefully other interfaces). In any case this is far in
the future for Fiji.

Albert
--
http://albert.rierol.net




More information about the ImageJ-devel mailing list