Exception with drawKeypoints & output mat

When I invoke the drawKeypoints function and I reuse the output Mat in imShow I get an exception. When I comment out drawKeypoints or just rename the output array it resolves the problem but I can’t use the output array with the rest of the program. I’ve narrowed it down to the output array of the drawKeypoints output array - or so I believe.

So this works fine:

Mat im_with_keypoints
..
ReadIMage 
{
   ...
   rawImage = BytestoMat(frameData->pu8ImageData, cameraCfg.u32Width, cameraCfg.u32Height);
				
im_with_keypoints = rawImage;
//drawKeypoints(rawImage, keypoints, im_with_keypoints, Scalar(0, 0, 255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
}
...
Main
{
...
if (im_with_keypoints.data == NULL)
			{
				cout << "No image found! Check path." << endl;

				//return 1;//ERROR
			}
			else
			{
			
				imshow("TEST--+", im_with_keypoints);
				//cvui::image(frame, 455, 25, target_still); //525, 500// <----here
				waitKey(1);//without this image won't be shown

			}

im_with_keypoints above appears only in those places above that are shown, throughout the program.

This throws the exception, when I enter the drawKeypoints command:
drawKeypoints(rawImage, keypoints, im_with_keypoints, Scalar(0, 0, 255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);

Mat im_with_keypoints
..
ReadIMage 
{
   ...
   rawImage = BytestoMat(frameData->pu8ImageData, cameraCfg.u32Width, cameraCfg.u32Height);
				
im_with_keypoints = rawImage;
*drawKeypoints(rawImage, keypoints, im_with_keypoints, Scalar(0, 0, 255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);*
...
}
Main
{
...
if (im_with_keypoints.data == NULL)
			{
				cout << "No image found! Check path." << endl;

				//return 1;//ERROR
			}
			else
			{
			
				imshow("TEST--+", im_with_keypoints);
				//cvui::image(frame, 455, 25, target_still); //525, 500// <----here
				waitKey(1); //without this image won't be shown

			}

Introduction of the 2nd array by drawKeypoints causes an exception with im_show below it but doesn’t without it.

I’ve tried researching the nature of the output array in drawKeypoints and different ways of defining the matrix but to no avail.

what does it do, exactly ?
if it constructs a Mat using frameData->pu8ImageData, as soon as the data pointer goes out of scope, it will be invalid (that’s why imshow() crashes)

your if (im_with_keypoints.data == NULL) fails, because there is something in data, it’s just invalid (points nowhere)

the Mat you keep around, should be a deep copy, like in

   rawImage = BytestoMat(frameData->pu8ImageData, cameraCfg.u32Width, cameraCfg.u32Height);			
   im_with_keypoints = rawImage.clone(); // do this immediately

Thanks Berak. I may have made a mistake in chronicling the order of things, for which I might have to apologize. I think what you are saying still applies, but check me:

If I create a Mat:

Mat rawImage;
Mat image_with_keypoints;

Then I draw on it with:

drawKeypoints(rawImage, keypoints, im_with_keypoints, Scalar(0, 0, 255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);

Then I immediately copy to

rawImage = BytestoMat(frameData->pu8ImageData, cameraCfg.u32Width, cameraCfg.u32Height);			
im_with_keypoints_copy = rawImage;  //(this step)

Then I try to show it with imshow:

imshow("Test Window", im_with_keypoints_copy)

The exception occurs using the copy (without cloning) and the exception doesn’t occur using the original. *I think that’s what your point is by recommending the deep copy (clone). *

your if (im_with_keypoints.data == NULL) fails, because there is something in data, it’s just invalid (points nowhere)

What would be a way to check then whether the frame is valid?

you need to provide complete (but minimal) source code. that means runnable or as close to runnable as possible. “slice-of-life” pseudocode, ordered or not, won’t do. especially BytestoMat needs to be revealed, as well as frameData and all interactions across its whole lifetime. you are still keeping a source of problems from us.

How to ‘close out’ the post. Answer found.