Skip to content

Cv.Load<CvHaarClassifierCascade>() causes memory leak #207

@djluck

Description

@djluck

Cv.Load<CvHaarClassifierCascade>() returns an object that when disposed, will not release memory allocated, probably because IsEnabledDispose is set to false.

The following code sample replicates and fixes the bug:

static void Main(string[] args)
{
    for (var i = 0; i < 10000; i++)
    {
        //A potential b.ug in OpenCVSharp marks CvHaarClassifierCascade.IsEnabledDispose = false, preventing the IDispoable pattern from 
        //cleaning up the memory allocated when loading the face detection features
        using (var res = Cv.Load<CvHaarClassifierCascade>("./haarcascade_frontalface_default.xml"))
        {
            //Calling this method cleans up the region of allocated memory correctly. 
                        //Comment out these two lines to reproduce the memory leak.
            var ptr = res.CvPtr;
            NativeMethods.cvReleaseHaarClassifierCascade(ref ptr);
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions