gocv icon indicating copy to clipboard operation
gocv copied to clipboard

dnn: Fix dangling pointers returned by `GetLayerNames`

Open deradiri opened this issue 2 years ago • 3 comments

GetLayerNames returns an array of char pointers to cstrings in a vector<string>; unfortunately, once the vector is out of scope, the strings are destroyed. GetLayerNames callers are then left with dangling pointers.

This change fixes the problem by expanding the strs buffer returned by GetLayerNames and copying the vector's cstrings into it.

Testing: See this comment

Here's an example of the bug in action:


	n := net.GetLayerNames()

	for i, l := range n {
		fmt.Printf("Layer %d: %s\n", i, l)
	}

Before fix:

$ go test -tags customenv,static
Layer 0: @�
Layer 1: 0�
...

After fix:

$ go test -tags customenv,static
Layer 0: detector/yolo-v3-tiny/Conv_12/BiasAdd/YoloRegion
Layer 1: detector/yolo-v3-tiny/Conv_9/BiasAdd/YoloRegion
...

deradiri avatar Jan 25 '22 05:01 deradiri

Yes, I see how the previous implementation was lacking. I just wonder however how this test here has been passing? https://github.com/hybridgroup/gocv/blob/5bde20d79aa5c2376cb2ea16439252582321db28/dnn_test.go#L59

deadprogram avatar Mar 09 '22 09:03 deadprogram

@deadprogram Good question... I guess maybe it's that the the caffe backend keeps the layer names around for longer/permanently?

I discovered this issue using openvino, will see if I can add a test case. Is there some procedure for adding models as test artifacts?

deradiri avatar Mar 26 '22 10:03 deradiri

Yes, I see how the previous implementation was lacking. I just wonder however how this test here has been passing?

https://github.com/hybridgroup/gocv/blob/5bde20d79aa5c2376cb2ea16439252582321db28/dnn_test.go#L59

@deadprogram It turns out this test passes by pure luck, I ran it a few times and logged all the layers. Below is a diff between two runs:

image

The layer name at index 1 seems to always be intact, so rather than add a new model I'll update the test to check a set of layers distributed throughout the array. Diff of runs with and without the fix in this PR is below: image

deradiri avatar Jul 21 '22 09:07 deradiri