opencvsharp icon indicating copy to clipboard operation
opencvsharp copied to clipboard

[Question] Will the `Mat` implicit convert to `InputArray` cause memory leak?

Open GF-Huang opened this issue 2 years ago • 2 comments

Summary of your issue

Such like this, the mat2 implicit convert to MatchTemplate first parameter InputArray:

mat1.MatchTemplate(mat2, xxx);

Environment

Win11 22H2 .NET 7.

GF-Huang avatar Apr 24 '23 20:04 GF-Huang

The implicit conversion from Mat to InputArray allocates a small amount of memory space that is difficult to free by GC. (I believe this is a design error in the current OpenCvSharp.)

A good way to deal with this problem is to frequently call GC.Collect();. Alternatively, I recommend the following:

using var src = InputArray.Create(mat1);
using var dst = OutputArray.Create(mat2);
using var xxx_ = InputArray.Create(xxx);
Cv2.MatchTemplate(src, dst, xxx_);

shimat avatar May 06 '23 09:05 shimat

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Dec 15 '23 06:12 stale[bot]