EnoxSoftware / OpenCVForUnity

OpenCV for Unity (Untiy Asset Plugin)
https://assetstore.unity.com/packages/tools/integration/opencv-for-unity-21088
550 stars 172 forks source link

[2.6.0] fastTexture2DToMat has memory leak #188

Closed Rombond closed 1 week ago

Rombond commented 1 month ago

After using fastTexture2DToMat in the Update loop i've got a lot of memory leak. (Editor and Build)

I used https://github.com/Unity-Technologies/UnityBlackmagicVideo/tree/main to get a renderTexture of my camera Size: 1280x720 Format: ARGBHalf

[SerializeField] InputVideoDeviceHandle inputDevice;
RenderTexture renderTexture;
int width = renderTexture.width;
int height = renderTexture.height;
Mat rgbaMat = new Mat(height, width, CvType.CV_8UC4);
Mat grayMat = new Mat(height, width, CvType.CV_8UC1);
Texture2D rgbaTexture = new Texture2D(rgbaMat.width(), rgbaMat.height(), TextureFormat.ARGB32, false);

void Update()
{
    if (inputDevice.IsActive())
    {
        if (inputDevice.TryGetRenderTexture(out renderTexture))
        {
            Utils.textureToTexture2D(renderTexture, rgbaTexture);
            Utils.fastTexture2DToMat(rgbaTexture, rgbaMat);
            Imgproc.cvtColor(rgbaMat, grayMat, Imgproc.COLOR_RGBA2GRAY);
        }
    }
}

It doesn't happen with texture2DToMat but it is too slow for the Update loop. In profiler and memory profiler, memory leak is in "Untracked" type.

EnoxSoftware commented 1 week ago

@Rombond Upon investigation, a bug was found in the C# and C++ data passing process code. OpenCVForUnity 2.6.1 has already been fixed and updated. Thank you for your report.