# Exception at memory location error

**URL:** <https://forum.opencv.org/t/exception-at-memory-location-error/6836>\
**Category:** C++\
**Tags:** videoio\
**Created:** [December 22, 2021, 10:00am UTC](https://forum.opencv.org/t/exception-at-memory-location-error/6836 "2021-12-22T10:00:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![J.Elena.Suh](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@J.Elena.Suh](https://forum.opencv.org/u/J.Elena.Suh)\
**Post date:** [December 22, 2021, 10:00am UTC](https://forum.opencv.org/t/exception-at-memory-location-error/6836/1 "2021-12-22T10:00:16Z")

</div>

Hello,  
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.

```auto
#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;
				break;
			}
			if ((time(NULL) - startTime) >= runTime) {
				break;
			}
		}

		cap0.release();
		destroyAllWindows();

		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:

```auto
#ifndef HAVE_OPENGL
    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,  
JESuh

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [December 22, 2021, 12:39pm UTC](https://forum.opencv.org/t/exception-at-memory-location-error/6836/2 "2021-12-22T12:39:36Z")

</div>

2.4 is eol, and no more maintained

throw it away, and [try with latest release](https://github.com/opencv/opencv/releases/tag/4.5.4)

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [December 22, 2021, 1:24pm UTC](https://forum.opencv.org/t/exception-at-memory-location-error/6836/3 "2021-12-22T13:24:11Z")

</div>

> [@J.Elena.Suh](#):
>
> ```auto
> VideoCapture cap0(CV_CAP_DSHOW);
> 
> ```

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

```
	VideoCapture cap0(0, CV_CAP_DSHOW);

```

also:

```auto
		cap0 >> img;

```

there should be a check after reading, like:

```
if (img.empty()) break;

```

---

<div class="post-metadata">

**Author:** ![J.Elena.Suh](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@J.Elena.Suh](https://forum.opencv.org/u/J.Elena.Suh)\
**Post date:** [December 24, 2021, 5:32am UTC](https://forum.opencv.org/t/exception-at-memory-location-error/6836/4 "2021-12-24T05:32:54Z")

</div>

Thank you for the reply 🙂  
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.

```auto
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

```auto
VideoCapture cap0(CV_CAP_DSHOW + id);

```

I added

```auto
if (img.empty()) break;

```

and now the problem resolved.  
Thank you so much 🙂

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,  
JESuh
