The WaveFileReader constructor that opens a file using a filename calls into the other constructor taking a stream. This other constructor calls a method to verify the RIFF header, but when this fails, an exception is thrown back to the caller.
This has the unfortunate effect that the first constructor, given the filename, doesn't get a chance to close the stream it opened. The stream is thus left open until GC or similar takes care of it.
The best way to handle this would be to do the following:
1. Refactor out the contents of the constructor taking a stream into a new, private, constructor, that also takes the `ownInput` value as a parameter.
2. Change the two existing constructors to chain to the new one, passing true/false for that new parameter as appropriate
3. The new constructor would have a try/catch block around it (or at least around the stream construction and RIFF header check), and if exception was thrown in the RIFF header validation, and ownInput is true, dispose of the stream before throwing the exception up the chain.
Comments: thanks for reporting, will try to get this fixed for a future NAudio
This has the unfortunate effect that the first constructor, given the filename, doesn't get a chance to close the stream it opened. The stream is thus left open until GC or similar takes care of it.
The best way to handle this would be to do the following:
1. Refactor out the contents of the constructor taking a stream into a new, private, constructor, that also takes the `ownInput` value as a parameter.
2. Change the two existing constructors to chain to the new one, passing true/false for that new parameter as appropriate
3. The new constructor would have a try/catch block around it (or at least around the stream construction and RIFF header check), and if exception was thrown in the RIFF header validation, and ownInput is true, dispose of the stream before throwing the exception up the chain.
Comments: thanks for reporting, will try to get this fixed for a future NAudio