Access violation error when using -operator

Keep getting an access violation error every time I try and take one matrix from another in my main for loop. (Large - Wld).. Can anyone offer any guidance as to why and how I can fix it? Been stuck on this for ages now, can't grasp it. I've pasted my loop down below along with my - operator class. Can provide more if necessary. Tried adding various deletes[] and stuff as I thought it could be to do with memory leaks and such, really having an issue. Thanks!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
	for (int a = 0; a < M-49; a+=1 
			{
				for (int b = 0; b < N-36; b+=1)
				{
					Matrix Large = Scene.splitter(a, b); //create a matrix to use the scene data

					int ssd = 0; //creating a variable for ssd (sum of square diff)
					Matrix value = Large - Wld; //find the difference
					Matrix value2 = value * value; //square the value
			 
						for (int m = 0; m < L; m++) 
						{ 
							Large.~Matrix(); 
							ssd += value2._data[m]; 
						}
			
							if (ssd < highest) //looking for the smallest value
							{
								highest = ssd; 
								xBlock = a; 
								yBlock = b;
							} 
				}
			}

			return 0;
		}



			Matrix Matrix::operator-(const Matrix& other) //overload allowing -ing of matrices
			{	
				for (int i = 0; i < (_M*_N); i++)
				{
					this->_data[i] = this->_data[i] - other._data[i];
				}

				return *this;
			}


Last edited on
On line 13: Large.~Matrix();

Large has automatic storage duration, which means the object would be destroyed twice (once explicitly, once when it goes out-of-scope). Calling a destructor on an object whose lifetime is ended causes undefined behavior.

In general, you should not explicitly invoke destructors unless you can guarantee destruction isn't going to occur more than once. Examples to this effect usually occur around uses of the placement-new operator, which begins an object's dynamic lifetime independent of the memory it's constructed in. In practice, this means you should almost never call destructors manually.
Last edited on
@mbozzi I've actually got that removed in my current version, forgot to remove it on here before posting. I'm still getting the same error though. I only tried to add it as sort of a last resort to see if I could solve my error, my bad!


Okay so I've also found out that *(this->_data) - expression cannot be evaluated. Could this be the source of my issues? Can anyone help me solve this?
Last edited on
What is _M and _N on line 33?
What are the magic numbers 49 and 36 in the outer loop on line 1?
Are you sure both matrices are the same size, and big enough?
@mbozzi yeah the matrices are the same size, I've double checked that. They should also be big enough. _M and _N are equal to 49 and 36. Those figures are the rows/cols of the block that's being used to find the best match throughout a larger image.
Okay, what is Wld?
Can you create and post a small example which exhibits this error?
Last edited on
@mbozzi So Wld is the image I'm searching for inside the Large. They're both stored in matrices.

https://gyazo.com/50752f84cc6f37a73c1df5d771977eab

https://gyazo.com/67cec2f4265b88358343ce8209b8e9d8

I can provide more code if you need? Really struggling at this point so your help is greatly appreciated man!
Last edited on
> I've actually got that removed in my current version, forgot to remove it on here before posting.
here's an idea, post your actual code.

> Can provide more if necessary.
provide enough to reproduce your issue.
use github to upload.

> Tried adding various deletes[] and stuff
ah, random programming.

> this->_data[i] = this->_data[i] - other._data[i];
that would be fine for operator -=, not for operator -

> Matrix value2 = value * value; //square the value
I'll guess that that is not matrix multiplication, but element-by-element multiplication.

> if (ssd < highest) //looking for the smallest value
¿so `highest' stores the smallest value?

> So Wld is the image I'm searching for inside the Large.
A little context may help.
@ne555 thanks for your reply.

Okay so essentially, I have two .txt image files one of them happens to be a reference image of Wally from Where's Wally and the other the scene with him inside. Using a nearest neighbour search (in this case sum of square diff was used) I'm trying to find the best match and print it out. Now, it was all going pretty tidy until I started receiving these errors:

https://gyazo.com/50752f84cc6f37a73c1df5d771977eab

https://gyazo.com/67cec2f4265b88358343ce8209b8e9d8

Which seems to happen when taking one of the matrices from the other, which should be done for part of the algorithm. Everything is fine up until that point.

Below is a pastebin of the main loop which calls the -operation
http://pastebin.com/zCDgkum6

and then a portion of my matrix class
http://pastebin.com/Vt7scN1f

This includes my operator overloads and also the splitter function, which is the only other thing touching Large/scenelarge.

>random programming because I'm losing my mind :))
Last edited on
double delete, uninitialized pointer, out of bounds access...
run your program through valgrind to detect it.

your operator= fails with self-assignment.
need to check your copy constructor.
It wouldn't be necessary if you've used std::vector (or std::valarray, or cv::Mat)
Topic archived. No new replies allowed.