Skip to content

Fix bugs and memory leaks in yolov8 example#1349

Merged
deadprogram merged 7 commits intohybridgroup:devfrom
swdee:fix-yolov8-example
Jan 4, 2026
Merged

Fix bugs and memory leaks in yolov8 example#1349
deadprogram merged 7 commits intohybridgroup:devfrom
swdee:fix-yolov8-example

Conversation

@swdee
Copy link
Contributor

@swdee swdee commented Dec 20, 2025

As reported in #1338 the yolov8 detection example program has a memory leak.

This was confirmed with simple profiling.

Start reading device: /dev/video0
initial MatProfile count: 1680101
gocv.io/x/gocv.Mat profile: total 1680101
840000 @ 0x4ef405 0x4ef393 0x4f3445 0x4f2c45 0x4f254c 0x457d0b 0x48e0a1
#       0x4ef404        gocv.io/x/gocv.newMat+0x84              /home/shanon/go/pkg/mod/gocv.io/x/gocv@v0.42.0/mat_profile.go:71
#       0x4ef392        gocv.io/x/gocv.(*Mat).RowRange+0x12     /home/shanon/go/pkg/mod/gocv.io/x/gocv@v0.42.0/core.go:2550
#       0x4f3444        main.performDetection+0x344             /home/shanon/devel/code/local/gocv-yolov8-mem-leak/original.go:179
#       0x4f2c44        main.detect+0x2e4                       /home/shanon/devel/code/local/gocv-yolov8-mem-leak/original.go:142
#       0x4f254b        main.main+0x6eb                         /home/shanon/devel/code/local/gocv-yolov8-mem-leak/original.go:99
#       0x457d0a        runtime.main+0x28a                      /usr/lib/go-1.24/src/runtime/proc.go:283

840000 @ 0x4ef4c5 0x4ef453 0x4f347f 0x4f2c45 0x4f254c 0x457d0b 0x48e0a1
#       0x4ef4c4        gocv.io/x/gocv.newMat+0x84              /home/shanon/go/pkg/mod/gocv.io/x/gocv@v0.42.0/mat_profile.go:71
#       0x4ef452        gocv.io/x/gocv.(*Mat).ColRange+0x12     /home/shanon/go/pkg/mod/gocv.io/x/gocv@v0.42.0/core.go:2558
#       0x4f347e        main.performDetection+0x37e             /home/shanon/devel/code/local/gocv-yolov8-mem-leak/original.go:181
#       0x4f2c44        main.detect+0x2e4                       /home/shanon/devel/code/local/gocv-yolov8-mem-leak/original.go:142
#       0x4f254b        main.main+0x6eb                         /home/shanon/devel/code/local/gocv-yolov8-mem-leak/original.go:99
#       0x457d0a        runtime.main+0x28a                      /usr/lib/go-1.24/src/runtime/proc.go:283

100 @ 0x4eece5 0x4eec73 0x4f32e5 0x4f2c45 0x4f254c 0x457d0b 0x48e0a1
#       0x4eece4        gocv.io/x/gocv.newMat+0x84              /home/shanon/go/pkg/mod/gocv.io/x/gocv@v0.42.0/mat_profile.go:71
#       0x4eec72        gocv.io/x/gocv.(*Mat).Reshape+0x12      /home/shanon/go/pkg/mod/gocv.io/x/gocv@v0.42.0/core.go:659
#       0x4f32e4        main.performDetection+0x1e4             /home/shanon/devel/code/local/gocv-yolov8-mem-leak/original.go:175
#       0x4f2c44        main.detect+0x2e4                       /home/shanon/devel/code/local/gocv-yolov8-mem-leak/original.go:142
#       0x4f254b        main.main+0x6eb                         /home/shanon/devel/code/local/gocv-yolov8-mem-leak/original.go:99
#       0x457d0a        runtime.main+0x28a                      /usr/lib/go-1.24/src/runtime/proc.go:283

1 @ 0x4ee8c5 0x4ee8c6 0x4f212d 0x457d0b 0x48e0a1
#       0x4ee8c4        gocv.io/x/gocv.newMat+0x44      /home/shanon/go/pkg/mod/gocv.io/x/gocv@v0.42.0/mat_profile.go:71
#       0x4ee8c5        gocv.io/x/gocv.NewMat+0x45      /home/shanon/go/pkg/mod/gocv.io/x/gocv@v0.42.0/core.go:243
#       0x4f212c        main.main+0x2cc                 /home/shanon/devel/code/local/gocv-yolov8-mem-leak/original.go:67
#       0x457d0a        runtime.main+0x28a              /usr/lib/go-1.24/src/runtime/proc.go:283

initial MatProfile count: 3360201
gocv.io/x/gocv.Mat profile: total 3360201
1680000 @ 0x4ef405 0x4ef393 0x4f3445 0x4f2c45 0x4f254c 0x457d0b 0x48e0a1
#       0x4ef404        gocv.io/x/gocv.newMat+0x84              /home/shanon/go/pkg/mod/gocv.io/x/gocv@v0.42.0/mat_profile.go:71
#       0x4ef392        gocv.io/x/gocv.(*Mat).RowRange+0x12     /home/shanon/go/pkg/mod/gocv.io/x/gocv@v0.42.0/core.go:2550
#       0x4f3444        main.performDetection+0x344             /home/shanon/devel/code/local/gocv-yolov8-mem-leak/original.go:179
#       0x4f2c44        main.detect+0x2e4                       /home/shanon/devel/code/local/gocv-yolov8-mem-leak/original.go:142
#       0x4f254b        main.main+0x6eb                         /home/shanon/devel/code/local/gocv-yolov8-mem-leak/original.go:99
#       0x457d0a        runtime.main+0x28a                      /usr/lib/go-1.24/src/runtime/proc.go:283

1680000 @ 0x4ef4c5 0x4ef453 0x4f347f 0x4f2c45 0x4f254c 0x457d0b 0x48e0a1
#       0x4ef4c4        gocv.io/x/gocv.newMat+0x84              /home/shanon/go/pkg/mod/gocv.io/x/gocv@v0.42.0/mat_profile.go:71
#       0x4ef452        gocv.io/x/gocv.(*Mat).ColRange+0x12     /home/shanon/go/pkg/mod/gocv.io/x/gocv@v0.42.0/core.go:2558
#       0x4f347e        main.performDetection+0x37e             /home/shanon/devel/code/local/gocv-yolov8-mem-leak/original.go:181
#       0x4f2c44        main.detect+0x2e4                       /home/shanon/devel/code/local/gocv-yolov8-mem-leak/original.go:142
#       0x4f254b        main.main+0x6eb                         /home/shanon/devel/code/local/gocv-yolov8-mem-leak/original.go:99
#       0x457d0a        runtime.main+0x28a                      /usr/lib/go-1.24/src/runtime/proc.go:283

200 @ 0x4eece5 0x4eec73 0x4f32e5 0x4f2c45 0x4f254c 0x457d0b 0x48e0a1
#       0x4eece4        gocv.io/x/gocv.newMat+0x84              /home/shanon/go/pkg/mod/gocv.io/x/gocv@v0.42.0/mat_profile.go:71
#       0x4eec72        gocv.io/x/gocv.(*Mat).Reshape+0x12      /home/shanon/go/pkg/mod/gocv.io/x/gocv@v0.42.0/core.go:659
#       0x4f32e4        main.performDetection+0x1e4             /home/shanon/devel/code/local/gocv-yolov8-mem-leak/original.go:175
#       0x4f2c44        main.detect+0x2e4                       /home/shanon/devel/code/local/gocv-yolov8-mem-leak/original.go:142
#       0x4f254b        main.main+0x6eb                         /home/shanon/devel/code/local/gocv-yolov8-mem-leak/original.go:99
#       0x457d0a        runtime.main+0x28a                      /usr/lib/go-1.24/src/runtime/proc.go:283

1 @ 0x4ee8c5 0x4ee8c6 0x4f212d 0x457d0b 0x48e0a1
#       0x4ee8c4        gocv.io/x/gocv.newMat+0x44      /home/shanon/go/pkg/mod/gocv.io/x/gocv@v0.42.0/mat_profile.go:71
#       0x4ee8c5        gocv.io/x/gocv.NewMat+0x45      /home/shanon/go/pkg/mod/gocv.io/x/gocv@v0.42.0/core.go:243
#       0x4f212c        main.main+0x2cc                 /home/shanon/devel/code/local/gocv-yolov8-mem-leak/original.go:67
#       0x457d0a        runtime.main+0x28a              /usr/lib/go-1.24/src/runtime/proc.go:283

The PR fixes the memory leak and some other bugs.

  • There is a memory leak in the gocv.TransposeND(outs[0], []int{0, 2, 1}, &outs[0]) call. When &outs[0] is passed as the destination, you end up overwriting the original Mat in outs[0] with a new allocation and you lose the only handle to the old allocation without outs[0].Close() being called.
  • As I was only looking at the code on github, it originally did not execute as the classes variable was not defined. I initially missed seeing it was in a separate file. To avoid others doing this I have removed it and defined it in the main program.
  • There were memory leaks on the ColRange(), Cols(), and RowRange() function calls as they were not being closed.
  • There was a bug on the NMS boxes range that skipped the first box, as index=0 is a valid NMS box.
  • The NMS box coordinates have been fixed which was using variable cols to get their position, instead of the fixed/absolute positions 0 to 3. This has resolved buggy detection of objects per frame.
  • Also added a handle for quiting the program when mouse clicks the window close icon.

After the above fixes, Mat profiling reveals all memory leaks are fixed.

initial MatProfile count: 1
gocv.io/x/gocv.Mat profile: total 1
1 @ 0x4ee8c5 0x4ee8c6 0x4f212d 0x457d0b 0x48e0a1
#       0x4ee8c4        gocv.io/x/gocv.newMat+0x44      /home/shanon/go/pkg/mod/gocv.io/x/gocv@v0.42.0/mat_profile.go:71
#       0x4ee8c5        gocv.io/x/gocv.NewMat+0x45      /home/shanon/go/pkg/mod/gocv.io/x/gocv@v0.42.0/core.go:243
#       0x4f212c        main.main+0x2cc                 /home/shanon/devel/code/local/gocv-yolov8-mem-leak/fixed.go:67
#       0x457d0a        runtime.main+0x28a              /usr/lib/go-1.24/src/runtime/proc.go:283

@deadprogram
Copy link
Member

Hello @swdee thanks for looking into this.

Other than the issue with closing right away that I mentioned above, it does in fact work better. 😄

…sistent across desktop backends and results in immediate program exit on some
@deadprogram
Copy link
Member

Thank you very much for the corrections/improvements @swdee now squash/merging.

@deadprogram deadprogram merged commit 96896bb into hybridgroup:dev Jan 4, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants