hybridgroup / gocv

Go package for computer vision using OpenCV 4 and beyond. Includes support for DNN, CUDA, OpenCV Contrib, and OpenVINO.
https://gocv.io
Other
6.75k stars 869 forks source link

opencv4.5.3 : Memory leak issue in IMEncodeWithParams #902

Open q3chang opened 3 years ago

q3chang commented 3 years ago

opencv4.5.3 : Memory leak issue in IMEncodeWithParams

Description

\ Although I use defer buffer.Close(), IMEncodeWithParams cause memory leak in streaming. If buffer.Close() is executed after declaring a buffer in a for loop to catch the memory leak, the issue that streaming does not work properly occurs. ## Steps to Reproduce

Below environment cause memory leak :

var frame *gocv.NativeByteBuffer
defer frame.Close()

for stream_is{
        params := []int{gocv.IMWriteJpegQuality, 90}
        frame, _ := gocv.IMEncodeWithParams(gocv.JPEGFileExt, img, params)
        select {
        case c.Frame <- frame:

        default:
            c.Frame = make(chan []byte, BUFFER_NUM)
            fmt.Println("buffer has been flushed...")
        }
        frame.Close()
}

Below environment cause streaming error :

for stream_is{
        params := []int{gocv.IMWriteJpegQuality, 90}
        frame, _ := gocv.IMEncodeWithParams(gocv.JPEGFileExt, img, params)
        select {
        case c.Frame <- frame:

        default:
            c.Frame = make(chan []byte, BUFFER_NUM)
            fmt.Println("buffer has been flushed...")
        }
        frame.Close()
}

Your Environment

deadprogram commented 3 years ago

Sorry, I cannot really understand the nature of your error from just the code you present. However seems like something external to GoCV? For sure, Close() needs to be called on each NativeByteBuffer returned by each call to gocv.IMEncodeWithParams().