Skip to content
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

Closed
yangzhongke opened this issue Dec 17, 2020 · 6 comments · Fixed by #1110
Labels
enhancement New feature or request

Comments

@yangzhongke
Copy link
Contributor

yangzhongke commented Dec 17, 2020

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:

using (ResourceTracker t = new ResourceTracker())
{
	Mat mat1 = t.NewMat(new Size(100, 100), MatType.CV_8UC3,new Scalar(0));
	Mat mat3 = t.T(255-t.T(mat1*0.8));
	Mat[] mats1 = t.T(mat3.Split());
	Mat mat4 = t.NewMat();
	Cv2.Merge(new Mat[] { mats1[0], mats1[1], mats1[2] }, mat4);
}

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

@shimat shimat added the enhancement New feature or request label Dec 17, 2020
@shimat
Copy link
Owner

shimat commented Dec 17, 2020

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.

@yangzhongke
Copy link
Contributor Author

I have pulled a request in #1110
How can I contribute to the document? I didn't find any related file under the folder 'doc'.
thanks

@shimat
Copy link
Owner

shimat commented Dec 22, 2020

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.

@yangzhongke
Copy link
Contributor Author

Thanks for your work.
I don't think modifying README.md right now is a good idea, because the new version of NuGet package has not been released yet, if we change README.md now, the users will cannot use ResourcesTracker. Therefore, I think it is best to modify README.md when new NuGet package is released. When will you release new NuGet package?

@shimat
Copy link
Owner

shimat commented Dec 26, 2020

I have just released a new NuGet package, which includes ResourceTracker.

@yangzhongke
Copy link
Contributor Author

Thanks, I have created a PullRequest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants