-
Notifications
You must be signed in to change notification settings - Fork 21
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
Fixup management of managed and unmanaged resources #101
base: master
Are you sure you want to change the base?
Fixup management of managed and unmanaged resources #101
Conversation
The NngDialer.NativeNngStruct is an unmanaged resource and therefore needs to always be cleaned up in the protected virtual void Dispose(bool) method. https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose#the-disposebool-method-overload Also, since this class contains an unmanaged resource a finalizer must also be provided. https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose#implement-the-dispose-pattern
The NngSocket.NativeNngStruct is an unmanaged resource and therefore needs to always be cleaned up in the protected virtual void Dispose(bool) method. https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose#the-disposebool-method-overload Also, since this class contains an unmanaged resource a finalizer must also be provided. https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose#implement-the-dispose-pattern
The NngDialer.NativeNngStruct is an unmanaged resource and therefore needs to always be cleaned up in the protected virtual void Dispose(bool) method. https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose#the-disposebool-method-overload Also, since this class contains an unmanaged resource a finalizer must also be provided. https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose#implement-the-dispose-pattern
The NngAlloc.Ptr is an unmanaged resource and therefore needs to always be cleaned up in the protected virtual void Dispose(bool) method. https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose#the-disposebool-method-overload Also, since this class contains an unmanaged resource a finalizer must also be provided. https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose#implement-the-dispose-pattern
The NngCtx.NativeNngStruct is an unmanaged resource and therefore needs to always be cleaned up in the protected virtual void Dispose(bool) method. https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose#the-disposebool-method-overload Also, since this class contains an unmanaged resource a finalizer must also be provided. https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose#implement-the-dispose-pattern
Managed objects should be conditionally disposed based on whether or not cleanup is deterministic or not. https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose#implement-the-dispose-pattern
Moved Finalize Dispose pattern from StatRoot class into its base class NngStat. The NngStat.NativeNngStruct is an unmanaged resource and therefore needs to always be cleaned up in the protected virtual void Dispose(bool) method. https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose#the-disposebool-method-overload Also, since this class contains an unmanaged resource a finalizer must also be provided. https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose#implement-the-dispose-pattern This also ensures that NngStat.NativeNngStruct is appropriately cleaned up in StatChild as well.
The NngString.Ptr is an unmanaged resource and therefore needs to always be cleaned up in the protected virtual void Dispose(bool) method. https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose#the-disposebool-method-overload Also, since this class contains an unmanaged resource a finalizer must also be provided. https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose#implement-the-dispose-pattern
Codecov Report
@@ Coverage Diff @@
## master #101 +/- ##
=======================================
Coverage 68.10% 68.10%
=======================================
Files 13 13
Lines 464 464
Branches 46 46
=======================================
Hits 316 316
Misses 132 132
Partials 16 16
Flags with carried forward coverage won't be shown. Click here to find out more. Continue to review full report at Codecov.
|
YES! |
WIP
🚧 👷 🏗️
... this is a work in progress but I want to create draft to provide an opportunity for discussion.
Background
This work stems primarily from reading the .NET documentation about implementing the "dispose pattern".
According to this section documentation, the implementation of a
void Dispose(bool disposing)
method should:IDisposable
objects) that executes only if invoked fromvoid Dispose()
The documentation also states that "If the method call comes from a finalizer, only the code that frees unmanaged resources should execute."
PR Summary
This PR is an attempt to fix some of the classes that must follow the "dispose pattern". The classes affected by this PR:
AsyncBase<T>
NngAlloc
NngCtx
NngDialer
NngListener
NngSocket
NngStat
NngString
Other Thoughts
In the process of working on this it appears that there are cases for an
IDisposable
to be "owned" by more than one entity which makes clean up difficult - which "owner" should be responsible for invokingDispose()
on the object? 🤷Example:
nng.RepSocket
AsyncBase<T>
class by passing thenng.RepSocket
toCreateRepReqAsyncContext()
ASyncBase<T>
"own" thenng.RepSocket
Since
nng.RepSocket
is anIDisposable
object, someone must invoke itsDispose()
method... but who?This type of problem appears to be a known limitation with
IDisposable
. A brief searching of the web shows some have implemented reference counting wrappers aroundIDisposable
objects as a solution (similar to C++ shared_ptr): https://stackoverflow.com/a/22092381Addressing this probably wont be a small task and seems to fall outside the scope of this PR (at least for now).