resource-leak

Do I need to call Close() on a ManualResetEvent?

瘦欲@ 提交于 2019-11-27 03:51:42
问题 I've been reading up on .NET Threading and was working on some code that uses a ManualResetEvent. I have found lots of code samples on the internet. However, when reading the documentation for WaitHandle, I saw the following: WaitHandle implements the Dispose pattern. See Implementing Finalize and Dispose to Clean Up Unmanaged Resources. None of the samples seem to call .Close() on the ManualResetEvent objects they create, even the nice Recursion and Concurrency article from the pfxteam blog

Does DataAdapter.Fill() close its connection when an Exception is thrown?

坚强是说给别人听的谎言 提交于 2019-11-27 01:56:28
问题 I am using ADO.NET (.NET 1.1) in a legacy app. I know that DataAdapter.Fill() opens and closes connections if the connection hasn't been opened manually before it's given to the DataAdapter. My question: Does it also close the connection if the .Fill() causes an Exception? (due to SQL Server cannot be reached, or whatever). Does it leak a connection or does it have a built-in Finally-clause to make sure the connection is being closed. Code Example: Dim cmd As New SqlCommand Dim da As New

Why is Files.lines (and similar Streams) not automatically closed?

泄露秘密 提交于 2019-11-27 01:06:52
The javadoc for Stream states: Streams have a BaseStream.close() method and implement AutoCloseable, but nearly all stream instances do not actually need to be closed after use. Generally, only streams whose source is an IO channel (such as those returned by Files.lines(Path, Charset)) will require closing. Most streams are backed by collections, arrays, or generating functions, which require no special resource management. (If a stream does require closing, it can be declared as a resource in a try-with-resources statement.) Therefore, the vast majority of the time one can use Streams in a

Will stream classes or connections considered as a resource leak in Java

久未见 提交于 2019-11-26 21:25:23
问题 Java has no lifetime for an object, this is managed by the garbage collector . And if I use some IO classes without closing it, or some DBConnection , will this considered a resource leak? In another words, will IO object be collected and destroyed by garbage collector, AFAIK, the garbage collector is for memory only. For example: BufferedReader br = new BufferedReader( new FileReader( new File("path") ) ); 回答1: Yes you are right. Garbage collection frees Java heap (memory) but close() frees

Scanner is never closed

社会主义新天地 提交于 2019-11-26 16:14:12
问题 I'm working on a game and I came across a little problem with my scanner. I'm getting a resource leak scanner never closed. But I thought my scanner was working before without closing it. But now it ain't. Anyone can help me out here? import java.util.Scanner; public class Main { public static final boolean CHEAT = true; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int amountOfPlayers; do { System.out.print("Select the amount of players (1/2): "); while (

Why is Files.lines (and similar Streams) not automatically closed?

送分小仙女□ 提交于 2019-11-26 08:15:30
问题 The javadoc for Stream states: Streams have a BaseStream.close() method and implement AutoCloseable, but nearly all stream instances do not actually need to be closed after use. Generally, only streams whose source is an IO channel (such as those returned by Files.lines(Path, Charset)) will require closing. Most streams are backed by collections, arrays, or generating functions, which require no special resource management. (If a stream does require closing, it can be declared as a resource