From 120e11c613c6fc1fd3116e6903008cdfb7e03cfa Mon Sep 17 00:00:00 2001 From: "a.malinovskiy" Date: Thu, 16 Sep 2021 15:27:43 +0500 Subject: [PATCH 1/2] fix: panic if repeat defer mat.Close --- mat_noprofile.go | 3 +++ mat_profile.go | 3 +++ 2 files changed, 6 insertions(+) diff --git a/mat_noprofile.go b/mat_noprofile.go index 5cc82a38..6e904acf 100644 --- a/mat_noprofile.go +++ b/mat_noprofile.go @@ -20,6 +20,9 @@ func newMat(p C.Mat) Mat { // Close the Mat object. func (m *Mat) Close() error { + if m.p == nil { + return nil + } C.Mat_Close(m.p) m.p = nil m.d = nil diff --git a/mat_profile.go b/mat_profile.go index eaeaca8b..45190bb3 100644 --- a/mat_profile.go +++ b/mat_profile.go @@ -73,6 +73,9 @@ func newMat(p C.Mat) Mat { // Close the Mat object. func (m *Mat) Close() error { + if m.p == nil { + return nil + } // NOTE: The pointer must be removed from the profile before it is deleted to // avoid a data race. MatProfile.Remove(m.p) From 84f9c907aa97b7fe2d09ac2a8de7f847ba18b0f6 Mon Sep 17 00:00:00 2001 From: "a.malinovskiy" Date: Thu, 16 Sep 2021 16:05:51 +0500 Subject: [PATCH 2/2] fix: error for dublicate call Close --- mat_noprofile.go | 3 ++- mat_profile.go | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/mat_noprofile.go b/mat_noprofile.go index 6e904acf..0d6d56a5 100644 --- a/mat_noprofile.go +++ b/mat_noprofile.go @@ -7,6 +7,7 @@ package gocv #include "core.h" */ import "C" +import "errors" // addMatToProfile does nothing if matprofile tag is not set. func addMatToProfile(p C.Mat) { @@ -21,7 +22,7 @@ func newMat(p C.Mat) Mat { // Close the Mat object. func (m *Mat) Close() error { if m.p == nil { - return nil + return errors.New("duplicate Mat close") } C.Mat_Close(m.p) m.p = nil diff --git a/mat_profile.go b/mat_profile.go index 45190bb3..1caadd7c 100644 --- a/mat_profile.go +++ b/mat_profile.go @@ -74,7 +74,7 @@ func newMat(p C.Mat) Mat { // Close the Mat object. func (m *Mat) Close() error { if m.p == nil { - return nil + return errors.New("duplicate Mat close") } // NOTE: The pointer must be removed from the profile before it is deleted to // avoid a data race.