Qt signal slot cv::Mat unable to read memory access violation

浪子不回头ぞ 提交于 2019-12-01 14:31:58

What I see is happenning:

  1. You get an image from queue. As per OpenCV docs:

    Mat& cv::Mat::operator= ( const Mat & m )

Assigned, right-hand-side matrix. Matrix assignment is an O(1) operation. This means that no data is copied but the data is shared and the reference counter, if any, is incremented. Before assigning new data, the old data is de-referenced via Mat::release .

  1. Then you pass it as cv::Mat image (by value) when emitting signal. The copy constructor again doesn't copy any data:

Array that (as a whole or partly) is assigned to the constructed matrix. No data is copied by these constructors. Instead, the header pointing to m data or its sub-array is constructed and associated with it. The reference counter, if any, is incremented. So, when you modify the matrix formed using such a constructor, you also modify the corresponding elements of m . If you want to have an independent copy of the sub-array, use Mat::clone() .

  1. Your data pointers are queued on UI thread

  2. You get/try-get new frame triggering release from p.1

  3. Your queued slot is executed and crashes...

Suggestion: I haven't worked much with it, but it seems something like cv::Mat::clone to make a deep copy is what you need, to prevent release of memory before it would be used by UI thread.

Or possibly it would be enough to define image right when you pop it from queue:

cv::Mat image = camera_q->pop();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!