-
-
Notifications
You must be signed in to change notification settings - Fork 934
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
Move IDisposable implementation declaration from inheritees to parent #746
base: develop
Are you sure you want to change the base?
Conversation
… AuthenticationMethod
Dispose pattern should be implemented in accordance with Implement a Dispose method to avoid code duplication. |
Hey @IgorMilavec, thanks for the feedback. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks much cleaner now. Why not instantiate _authenticationCompleted in AuthenticationMethod's constructor? Also, do not declare finalizers, if they do not actually perform cleanup.
Because derived classes use different types for the EventWaitHandle : PrivateKeyAuthenticationMethod is using ManualResetEvent and the rest three are using AutoResetEvent.
Totally agree! Especially that classes with finalizers can take more time to dispose, so removing them is actaully small performance optimization. Updated code without finalizers on c34ad23. |
Huh, I totally missed different EventWaitHandles... I understand now. :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good to me.
Pleasure doing business with you. Thanks |
Hello,
I was using SSH.NET library in my project and I noticed that by following inheritance rules I fell into a trap.
My SFTP wrapper class contains private field of a general AuthenticationMethod type.
It is is assigned in different constructors depending on selected authentication method.
When I was implementing Dispose() for SftpFileHandler I noticed that AuthenticationMethod does not require disposing which seemed a bit suspicious to me since it may contain sensitive data that may require proper disposing.
So I browsed descendants of AuthenticationMethod to discover that all 4 of them implement IDisposable interface.
That being said in other to properly dispose the whole SftpFileHandler I need to do something like:
Which is always true...
Instead of just simply:
I would like to propose a small improvement that would make disposing requirement move visible thus making it easier to maintain a little bit more secure code.
My suggestion is to move IDisposable a level above -> to the AuthenticationMethod:
This way code analyzing tools would warn programmers before making a mistake of not disposing inheritee of AuthenticationMethod class.
I tested changes mentioned above in my code and they work as expected.
What do you think about this improvement?