-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Can I contribute a method to simplify the release of managed resources such as Mat in OpenCVSharp #1107
Comments
Thank you for your comment. As you say, disposing Mat has been a long-standing concern. I think your idea is a good one. I would be happy to receive your pull request. |
I have pulled a request in #1110 |
Thank you! I don't see any documentation or wiki that you can contribute, but I think it would be good to add it to the README.md. |
Thanks for your work. |
I have just released a new NuGet package, which includes ResourceTracker. |
Thanks, I have created a PullRequest. |
In OpenCVSharp, objects of classes such as Mat and MatExpr have unmanaged resources and need to be manually released by calling the Dispose() method. Worst of all, the +, -, *, and other operators create a new object each time, and these objects need to be disposed, or there will be a memory leak. But the code that these objects release looks very verbose.
So I wrote a ResourceTracker class to manage OpenCV's resources. The T() method of the ResourceTracker class is used to add an OpenCV object to the tracing records and then return the object. The T() method is simply a matter of adding the wrapped object to the tracing records and returning it.
The ResourceTracker implements the IDisposable interface, and when the Dispose() method is called, all resources tracked by the ResourceTracker are disposed. The T() method can trace an object or an array of objects. And this method NewMat() is like T(new Mat(...). Because the +, -, *, and other operators creates a new object each time, the resulting object at each step needs to be disposed, and they can be wrapped with T().For example: t.T(255 - t.T(picMat * 0.8))
Therefore, the verbose C# code above can be reduced to the following:
After leaving the scope of using block of the ResourceTracker, the resources of all the objects managed by the ResourceTracker will be released.
Source code repository:https://github.com/yangzhongke/Zack.OpenCVSharp.Ext
Can I contribute ResourceTracker.cs into OpenCVSharp, so that the OpenCVSharp users can use Mat with ease?
If permitted, I will pull request. If it cannot be merged into OpenCVSharp, can you add my project to documents of OpenCVSharp?
Thanks
The text was updated successfully, but these errors were encountered: