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!
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;
}
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.
@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?
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 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:
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.
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)