Java2Dart Resource Leak

 Java developers need to use try finally clause in order to prevent resources leak.

 

 try (BufferedReader br =
                   new BufferedReader(new FileReader(path))) {
        return br.readLine();
    }



Prior to Java SE 7 the code looked like

BufferedReader br = null;
try{
br =  new BufferedReader(new FileReader(path))
} finally{
	if (br == null) {
		br.close();
}
}



The following code will guaranty that br.close(), will be called when the try block is over.

DART too has resources which needs to be closed, in order to prevent leaks. For example, here and basically most of the dart:io library  requires calling close().
However the DART language does not support syntax which eliminates the finally pitfall.

 

  OutputStream out = null;
    try {
      out =  new File("c:\\log.txt").openOutputStream(FileMode.WRITE);
      out.writeString("FILE ACCESSED ${new Date.now()}");
      
    } finally{
      if (out != null){
        out.close();
      }
    }

 

Thank you for your interest!

We will contact you as soon as possible.

Send us a message

Oops, something went wrong
Please try again or contact us by email at info@tikalk.com