I'm writing a producer/consumer code that receives frame date from an external library.
Each frame is read in a callback function from an external library that runs in a parallel thread and is pushed into a Mat queue. I created another function that runs in a different thread that reads and pops each frame.
The problem is that I'm getting "Access violation reading location" when I'm trying to read the frame data from the queue.
I'm declaring theses variables globally:
queue<Mat> matQ;
OnFrameDataReceivedCB videoCB;
OnDeviceConnectStatusCB connectCB;
guide_usb_video_mode_e videoMode;
int width = 640;
int height = 512;
std::mutex mu;
Here goes the code for the callback function that pushes each frame data:
void OnVideoCallBack(const guide_usb_frame_data_t data) //callback function
{
if (data.frame_rgb_data_length > 0)
{
// Send the displayed data directly
unsigned char* rgbData;
Mat frame;
Size size = Size(width, height);
rgbData = data.frame_rgb_data;
frame = Mat(size, CV_8UC3, rgbData, Mat::AUTO_STEP);
if (mu.try_lock())
{
printf("producing...\n");
matQ.push(frame);
printf("free producing\n");
mu.unlock();
}
}
}
Here is the function that reads from the queue:
void OnHandleVideoData()
{
while (true)
{
try
{
if (matQ.size() <= 0)
{
chrono::milliseconds duration(200);
this_thread::sleep_for(duration);
continue;
}
if (mu.try_lock())
{
if (matQ.size() > 0)
{
printf("consuming...\n");
Size size = Size(width, height);
Mat frame = Mat(size, CV_8UC3);
frame = matQ.front().clone();
matQ.pop();
imwrite("frame.jpg", frame); //the access violation exception is thrown on this line
printf("free consuming\n");
mu.unlock();
}
}
}
catch (...)
{
}
}
}
I also tried to put the unsigned char* rgbData array into a queue instead of the Mat, but I got the same error.
What am I missing?