英文:
NSInternalInconsistencyException occurs intermittently when using gocv on macOS
问题
我有这段使用gocv(为OpenCV提供Go语言绑定)的代码。它在图像上简单地绘制了一些矩形并显示结果。
func main() {
resp, err := http.Get("http://localhost:6060/template-match")
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(string(body))
var data Response
err = json.Unmarshal(body, &data)
if err != nil {
panic(err)
}
srcImage := gocv.IMRead("./images/src1.jpg", gocv.IMReadColor)
for i := 0; i < len(data.Data); i++ {
gocv.Rectangle(&srcImage, data.Data[i], color.RGBA{R: 255}, 2)
}
window := gocv.NewWindow("match-result")
window.IMShow(srcImage)
gocv.WaitKey(0)
fmt.Println(data.Data, data.Msg)
}
我遇到了这个错误:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSWindow drag regions should only be invalidated on the Main Thread!'
我真的很困惑,因为这个错误并不是每次都发生。
英文:
I have this code using gocv(provides Go language bindings for OpenCV).
It simply draw some rectangles on the image and show the result.
func main() {
resp, err := http.Get("http://localhost:6060/template-match")
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(string(body))
var data Response
err = json.Unmarshal(body, &data)
if err != nil {
panic(err)
}
srcImage := gocv.IMRead("./images/src1.jpg", gocv.IMReadColor)
for i := 0; i < len(data.Data); i++ {
gocv.Rectangle(&srcImage, data.Data[i], color.RGBA{R: 255}, 2)
}
window := gocv.NewWindow("match-result")
window.IMShow(srcImage)
gocv.WaitKey(0)
fmt.Println(data.Data, data.Msg)
I'm getting this error:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSWindow drag regions should only be invalidated on the Main Thread!'
I'm really confused because this error does not occur every time
答案1
得分: 0
我还没有使用过这个库,但是看起来这是一个已知的问题(https://github.com/hybridgroup/gocv/issues/599#issuecomment-579112179)。由于你提到它只是偶尔发生,所以很可能是与线程上下文有关的错误。那里的答案(来自库的作者)指向了https://github.com/golang/go/wiki/LockOSThread,所以你的主包应该实现类似以下的代码:
func init() {
runtime.LockOSThread()
}
英文:
I've not used this library, but it looks like this is a known issue – and, since you mentioned it happens only sometimes, it certainly sounds like the error is dependent upon thread context. The answer there (from the library author) points to https://github.com/golang/go/wiki/LockOSThread, so your main package should implement something like
func init() {
runtime.LockOSThread()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论