From be8caa6d42474f930e9b36bf575f8ef612a7fec7 Mon Sep 17 00:00:00 2001 From: Andrii Kushch Date: Wed, 15 May 2019 00:09:28 +0200 Subject: [PATCH] Fix possible resource leak --- contrib/tracking_test.go | 7 +++--- imgproc.go | 52 +++++++++++++++++++++------------------- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/contrib/tracking_test.go b/contrib/tracking_test.go index 68812d00..514bbc05 100644 --- a/contrib/tracking_test.go +++ b/contrib/tracking_test.go @@ -44,8 +44,9 @@ func TestSingleTrackers(t *testing.T) { } for _, test := range tab { - defer test.tracker.Close() - - BaseTestTracker(t, test.tracker, test.name) + func() { + defer test.tracker.Close() + BaseTestTracker(t, test.tracker, test.name) + }() } } diff --git a/imgproc.go b/imgproc.go index f41c1e77..72d58834 100644 --- a/imgproc.go +++ b/imgproc.go @@ -1154,22 +1154,24 @@ func FillPoly(img *Mat, pts [][]image.Point, c color.RGBA) { points := make([]C.struct_Points, len(pts)) for i, pt := range pts { - p := (*C.struct_Point)(C.malloc(C.size_t(C.sizeof_struct_Point * len(pt)))) - defer C.free(unsafe.Pointer(p)) + func() { + p := (*C.struct_Point)(C.malloc(C.size_t(C.sizeof_struct_Point * len(pt)))) + defer C.free(unsafe.Pointer(p)) - pa := getPoints(p, len(pt)) + pa := getPoints(p, len(pt)) - for j, point := range pt { - pa[j] = C.struct_Point{ - x: C.int(point.X), - y: C.int(point.Y), + for j, point := range pt { + pa[j] = C.struct_Point{ + x: C.int(point.X), + y: C.int(point.Y), + } } - } - points[i] = C.struct_Points{ - points: (*C.Point)(p), - length: C.int(len(pt)), - } + points[i] = C.struct_Points{ + points: (*C.Point)(p), + length: C.int(len(pt)), + } + }() } cPoints := C.struct_Contours{ @@ -1433,22 +1435,24 @@ func DrawContours(img *Mat, contours [][]image.Point, contourIdx int, c color.RG cntrs := make([]C.struct_Points, len(contours)) for i, contour := range contours { - p := (*C.struct_Point)(C.malloc(C.size_t(C.sizeof_struct_Point * len(contour)))) - defer C.free(unsafe.Pointer(p)) + func() { + p := (*C.struct_Point)(C.malloc(C.size_t(C.sizeof_struct_Point * len(contour)))) + defer C.free(unsafe.Pointer(p)) - pa := getPoints(p, len(contour)) + pa := getPoints(p, len(contour)) - for j, point := range contour { - pa[j] = C.struct_Point{ - x: C.int(point.X), - y: C.int(point.Y), + for j, point := range contour { + pa[j] = C.struct_Point{ + x: C.int(point.X), + y: C.int(point.Y), + } } - } - cntrs[i] = C.struct_Points{ - points: (*C.Point)(p), - length: C.int(len(contour)), - } + cntrs[i] = C.struct_Points{ + points: (*C.Point)(p), + length: C.int(len(contour)), + } + }() } cContours := C.struct_Contours{