On 19 April 2011 06:35,  <bode...@apache.org> wrote:
> Author: bodewig
> Date: Tue Apr 19 05:35:04 2011
> New Revision: 1094856
>
> URL: http://svn.apache.org/viewvc?rev=1094856&view=rev
> Log:
> print a warning if finalize closes the archive
>
> Modified:
>    
> commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
>
> Modified: 
> commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
> URL: 
> http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java?rev=1094856&r1=1094855&r2=1094856&view=diff
> ==============================================================================
> --- 
> commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
>  (original)
> +++ 
> commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
>  Tue Apr 19 05:35:04 2011
> @@ -102,6 +102,11 @@ public class ZipFile {
>     private final ZipEncoding zipEncoding;
>
>     /**
> +     * File name of actual source.
> +     */
> +    private final String archiveName;
> +
> +    /**
>      * The actual data source.
>      */
>     private final RandomAccessFile archive;
> @@ -180,6 +185,7 @@ public class ZipFile {
>      */
>     public ZipFile(File f, String encoding, boolean useUnicodeExtraFields)
>         throws IOException {
> +        this.archiveName = f.getAbsolutePath();
>         this.encoding = encoding;
>         this.zipEncoding = ZipEncodingHelper.getZipEncoding(encoding);
>         this.useUnicodeExtraFields = useUnicodeExtraFields;
> @@ -214,7 +220,11 @@ public class ZipFile {
>      * @throws IOException if an error occurs closing the archive.
>      */
>     public void close() throws IOException {
> +        // this flag is only written here and read in finalize() which
> +        // can never be run in parallel.
> +        // no synchronization needed.

Are you sure?

If finalize() runs in a different thread from close(), then the field
should be volatile to ensure safe publication.

>         closed = true;
> +
>         archive.close();
>     }
>
> @@ -320,7 +330,11 @@ public class ZipFile {
>      */
>     protected void finalize() throws Throwable {
>         try {
> -            close();
> +            if (!closed) {
> +                System.err.println("Cleaning up unclosed ZipFile for archive 
> "
> +                                   + archiveName);
> +                close();
> +            }
>         } finally {
>             super.finalize();
>         }
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org
For additional commands, e-mail: dev-h...@commons.apache.org

Reply via email to