mrglavas 2004/06/06 16:33:40 Modified: java/samples/xni ObjectFactory.java Log: Fixing another potential memory leak. The input stream
used for loading properties may never be closed if an IOException is thrown while reading from it. Adding finally blocks so that the input stream will always be closed. Revision Changes Path 1.6 +32 -12 xml-xerces/java/samples/xni/ObjectFactory.java Index: ObjectFactory.java =================================================================== RCS file: /home/cvs/xml-xerces/java/samples/xni/ObjectFactory.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- ObjectFactory.java 6 Jun 2004 15:23:52 -0000 1.5 +++ ObjectFactory.java 6 Jun 2004 23:33:40 -0000 1.6 @@ -160,9 +160,10 @@ fLastModified = -1; fXercesProperties = null; } - + synchronized (ObjectFactory.class) { boolean loadProperties = false; + FileInputStream fis = null; try { // file existed last time if(fLastModified >= 0) { @@ -186,32 +187,51 @@ if(loadProperties) { // must never have attempted to read xerces.properties before (or it's outdeated) fXercesProperties = new Properties(); - FileInputStream fis = ss.getFileInputStream(propertiesFile); + fis = ss.getFileInputStream(propertiesFile); fXercesProperties.load(fis); - fis.close(); } - } catch (Exception x) { - fXercesProperties = null; - fLastModified = -1; + } catch (Exception x) { + fXercesProperties = null; + fLastModified = -1; // assert(x instanceof FileNotFoundException - // || x instanceof SecurityException) - // In both cases, ignore and continue w/ next location - } + // || x instanceof SecurityException) + // In both cases, ignore and continue w/ next location + } + finally { + // try to close the input stream if one was opened. + if (fis != null) { + try { + fis.close(); + } + // Ignore the exception. + catch (IOException exc) {} + } + } } if(fXercesProperties != null) { factoryClassName = fXercesProperties.getProperty(factoryId); } } else { + FileInputStream fis = null; try { - FileInputStream fis = ss.getFileInputStream(new File(propertiesFilename)); + fis = ss.getFileInputStream(new File(propertiesFilename)); Properties props = new Properties(); props.load(fis); - fis.close(); factoryClassName = props.getProperty(factoryId); } catch (Exception x) { // assert(x instanceof FileNotFoundException // || x instanceof SecurityException) // In both cases, ignore and continue w/ next location + } + finally { + // try to close the input stream if one was opened. + if (fis != null) { + try { + fis.close(); + } + // Ignore the exception. + catch (IOException exc) {} + } } } if (factoryClassName != null) { --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]