-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Close() should NEVER call Dispose() #111258
Comments
Tagging subscribers to this area: @dotnet/area-system-componentmodel |
They are usually alias, for example
This is also implementation detail of the disposable pattern.
Re-opening is usually not a thing. Reusable objects must be specially designed other than non-reusable ones. For really-reusable objects like
Ideally it should, but it's impractical to really get exception-free when operating with external resource. The common practice is to not let it run, by calling
Any IO operation like network can get exception from physical connection lost, and not properly handling the exception will crash application. How is it related to |
I have to disagree. SerialPort is Component you can put onto a Form, then assign a port, Open, Close, assign different port, Open, Close, ...reuse. Why not?
No Close or Dispose ever helped with SerialPort in .NET 2.0 upto .NET 4.0 (at least 3.5) when you unplugged the USB (with VCP), because it threw exception from finalizer in GC-thread, which crashed the app, no matter what you did. EDIT:
See no such thing in
|
The runtime/src/libraries/System.IO.Ports/src/System/IO/Ports/SerialStream.Windows.cs Lines 724 to 734 in 289aa17
Decompiling the implementation of .NET Framework 2.0, it only guards for In contrast, runtime/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/BufferedFileStreamStrategy.cs Lines 128 to 132 in 289aa17
It's not fatal. The finalizer of All of the problems come from the implementation of |
I apologyze for not knowing how to properly adress this thing, please forward where appropriate.
Close()
should NEVER callDispose()
Dispose()
orDispose(bool)
CAN callClose()
but NEVER the other way around!These (C/D) are two entirely different things, be it file or COM port or anything, especially when it is derived from
Component
.runtime/src/libraries/System.IO.Ports/src/System/IO/Ports/SerialPort.cs
Line 588 in 289aa17
There is bigger story about this, but the main logic is:
Close
should notDispose
, becauseDispose
can (and often does) more things likeGC.SuppressFinalize
runtime/src/libraries/System.ComponentModel.Primitives/src/System/ComponentModel/Component.cs
Line 74 in 289aa17
How are you supposed to re-
Open
it? That should work, right?....and
~Finalize()
should never throw an exception, right?Reality about SerialPort at least until .NET 4.5 when you unplug the USB - the whole App crashes and there was no defence.
At lest that got solved I hope:
runtime/src/libraries/System.IO.Ports/src/System/IO/Ports/SerialPort.cs
Line 593 in 289aa17
But wait, this gets called from
Close
as well, right?runtime/src/libraries/System.ComponentModel.Primitives/src/System/ComponentModel/Component.cs
Line 86 in 289aa17
runtime/src/libraries/System.ComponentModel.Primitives/src/System/ComponentModel/Component.cs
Line 89 in 289aa17
The text was updated successfully, but these errors were encountered: