Double* <Unable to read memory>

I am using Opencv in Visual Studio 2012 with C++ in a Console Application.

fastx and fasty are both pointers to image matrices, a cv::Mat. When i hit i=1194 fastJy crashes when assigning it a value. Using break points, it seems that fasty has a value of '<Unable to read memory>' when the variable is inspected.

1
2
3
4
5
6
7
8
9
10
11
12
13
void IterationStage(cv::Mat Jx, cv::Mat Jy)
{
    double* fastJx;
    double* fastJy;
    for(int i=0; i<_count;i++)
    {
        fastJx = Jx.ptr<double>(0) + Jx.step*i;
	fastJy = Jy.ptr<double>(0) + Jy.step*i;
		
	fastJx[0] = 255;
	fastJx[1] = 255;
    }
}


Making this change fixes fastJy.

1
2
3
4
5
6
7
8
9
10
11
12
13
void IterationStage(cv::Mat Jx, cv::Mat Jy)
{
    double* fastx;
    double* fasty;
    for(int i=0; i<_count;i++)
    {
        fastJx = Jx.ptr<double>(0) + Jx.step*i;
	fastJy = Jx.ptr<double>(0) + Jy.step*i;  //CHANGED HERE.
		
	fastJx[0] = 255;
	fastJx[1] = 255;
    }
}


This is odd as i am still using the Jy.step with the Jx matrix. Both matrices are initialized with the same constructor, just before the function is calledas shown below.

1
2
3
cv::Mat Jx = cv::Mat::zeros(__paw.nPix(), __shape.nModes()+4, CV_64FC1);
cv::Mat Jy = cv::Mat::zeros(__paw.nPix(), __shape.nModes()+4, CV_64FC1);
IterationStage( Jx, Jy)


Anyway, the error message is this.

"Unhandled exception at 0x003A5777 in OpencvShapeTest.exe: 0xC0000005: Access violation writing location 0x024290A0."
Last edited on
closed account (3hM2Nwbp)
You're passing default-constructed cv::Mat4 objects to the function. Look closely at line 3 of the last code block you posted. Also, you're passing by value. Does cv::Mat have valid copy / move constructors? Also...fastJx and fastJy are not declared identifiers in the code you posted.
Last edited on
I have corrected the mistakes in the above post so it reads correctly.

1) You're passing default-constructed cv::Mat4 objects to the function. Look closely at line 3 of the last code block you posted.

This was an error. This is a much simpler version of the issue without all of the variable names that would clutter and make the problem unclear. Apologies.

2) Also, you're passing by value. Does cv::Mat have valid copy / move constructors?

Would a reference be ok? OpenCV uses smart pointers so only headers are created when a cv::Mat is created. It is designed to minimise data duplication for large data.

3) Also...fastJx and fastJy are not declared identifiers in the code you posted.

As above, spelling mistakes on my part.

The issue is the access violation. It doesn't happen with Jx, just Jy. How do i even begin to investigate this?
http://www.cplusplus.com/forum/general/112111/
A testcase consisting of randomly copy&paste'd bits of code that you thought were relevant can obviously not reproduce the problem.
Topic archived. No new replies allowed.