Exception at memory location error

I’m trying to handle USB camera with opencv 2.4.11 VIdeoCapture. (With Visual Studio 2013, Windows 10)
I use 2 USB cameras, and switching them every time to time. However the program crashes quite often.
I’ve connected 2 USB Camera to USB ss(super speed) slots on the behind of my PC directly, via USB3.0 cable (less then 1 meter long).
I want to solve this problem and set switching duration as 10 seconds.

Here is the error message:

*On the command prompt of my program:
OpenCV Error: Assertion failed (size.width)>0 && size.height>0) in cv::imshow, file C:\builds\2_4_PackSlave-win64-vc12-shared\opencv\modules\highgui\srd\windows.cpp, line 261

*pop-up window on VS2013:
Unhandled exception at ‘somewhere’ in ‘my program.exe’:
Microsoft C++ exception: cv::Exception at memory location ‘somewhere’.

Breakpoint is where I use imshow.

Here is a part of my code.

#include <iostream>
#include <time.h>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;

int main(int argc, char** argv) {

	int runTime = 10; // sec
	bool flag = true;

	while (flag==true)	{
		time_t startTime = time(NULL);

		VideoCapture cap0(CV_CAP_DSHOW);
		if (!cap0.isOpened()) {
			printf("Can't open the camera.");
			return -1;
		printf("Camera0 opened.");

		Mat img;
		while (true) {
			cap0 >> img;

			imshow("Camera0", img);
			if (waitKey(1) == 27) {
				flag = false;
			if ((time(NULL) - startTime) >= runTime) {


		startTime = time(NULL);

                // same thing for the other camera, with VideoCapture cap0(CV_CAP_DSHOW + 1);


	return 0;

I tried to check window.cpp line 261:

    CV_Assert(size.width>0 && size.height>0);  // this is line 261
        Mat img = _img.getMat();
        CvMat c_img = img;
        cvShowImage(winname.c_str(), &c_img);

Well, but I don’t have any idea right now…

Is there any way instead of using try & catch but solve this problem fundamentally?
And please let me know if you need any further information.

Thank you in advance!

Best regards,

2.4 is eol, and no more maintained

throw it away, and try with latest release

you need a camera id(0,1,2,3,…), like in

	VideoCapture cap0(0, CV_CAP_DSHOW);


		cap0 >> img;

there should be a check after reading, like:

if (img.empty()) break;

Thank you for the reply :slight_smile:
Probably I’ll move to the latest version soon. I wanted to see if there is any problem using 2.4.11
Currently, this old version OpenCV is rooted in quite a big program and I need some effort to replace it to the latest version.

VideoCapture cap0(0, CV_CAP_DSHOW);

doesn’t work for OpenCV 2.4.11 (but for the later versions, works very well), and I used

VideoCapture cap0(CV_CAP_DSHOW + id);

I added

if (img.empty()) break;

and now the problem resolved.
Thank you so much :slight_smile:

I have another problem under this condition now.
After few hours of switching USB cameras,
it suddenly crashed without any useful error message.
I can see only the following messages

…The thread 0x1500 has exited with code -1 (0xffffffff).

The program ‘some program.exe’ has exited with code -1 (0xffffffff).

I’ll try to generate crash dump.

Thank you!

Best Regars,