gocv icon indicating copy to clipboard operation
gocv copied to clipboard

opencv4.5.3 : Memory leak issue in IMEncodeWithParams

Open jayden-dragon opened this issue 2 years ago • 1 comments

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

  • Operating System and version: ubuntu 18.04
  • OpenCV version used: 4.5.3
  • How did you install OpenCV?
  • GoCV version used: 0.28
  • Go version: 1.16.5
  • Did you run the env.sh or env.cmd script before trying to go run or go build? sure

jayden-dragon avatar Sep 30 '21 00:09 jayden-dragon

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().

deadprogram avatar Oct 27 '21 12:10 deadprogram