gocv icon indicating copy to clipboard operation
gocv copied to clipboard

How can I clear my memory? It's growing up!

Open brejce opened this issue 2 years ago • 1 comments

How can I clear my memory? It's growing up!There is another thing,sometime I can't use camera 0,just can’t,I don't know why?。

func main() {     initFyne(500, 500) } func initFyne(w, h int) {     W.Resize(fyne.NewSize((w), (h)))     A.Settings().SetTheme(theme.LightTheme())     cap, err := gocv.VideoCaptureDevice(1)     if err != nil {         panic(err)     }     defer cap.Close()     mat := gocv.NewMat()     defer mat.Close()     go func() {         for range time.Tick(time.Second) {             cap.Read(&mat)             img, err := mat.ToImage()             if err != nil {                 panic(err)             } //do something for this img             image := canvas.NewImageFromImage(img)             image.FillMode = canvas.ImageFillOriginal             W.SetContent(container.NewVBox(                 widget.NewLabel("hahahahahahahahahaha"),                 image,             ))         }     }()     W.ShowAndRun() }

Your Environment

  • Operating System and version:win11
  • OpenCV version used:4.5.5
  • How did you install OpenCV? I forgot, maybe I installed gocv after installing opencv separately
  • GoCV version used:0.30.0
  • Go version:1.18
  • Did you run the env.sh or env.cmd script before trying to go run or go build? not at all

brejce avatar May 20 '22 09:05 brejce

Hi,

for question : "sometime I can't use camera 0,just can’t..." Sometimes when you plug camera in the USB port. It is assigned as some ID which is used by opencv to access it. When your code crashes or something weird happen or when you remove USB cam to plug it back, the camera ID is not reinstated to its previous value, so it gets assigned to another one.

Q2. Regarding memory build up :

Line : "img, err := mat.ToImage()" For every iteration it's creating new memory buffer in CPU. Solution to it is close those Mat manually. Avoid defer also. Just close it manually

hiteshhedwig avatar Jun 10 '22 07:06 hiteshhedwig