jeudi 8 octobre 2020

Object variable changing after leaving void method [closed]

I am relatively new to C++ and I have a class called 'Image' which has an 'array' pointer as a private variable that points to an array of objects (Called Coordinates). I am using CLion and have set breakpoints both in the last line of the method being called as well as the next line in the method it was called from and when running in debug mode I am viewing the values in this array at both points and they are drastically different but the memory address is the same. Is there some fundamentals that I am missing that would do this? Like a common beginners mistake?

m_corners[0] = &top_points[0];
m_corners[1] = &top_points[1];

m_corners[2] = &bottom_points[0];
m_corners[3] = &bottom_points[1];


if(m_corners[0]->getX() > m_corners[1]->getX()){
    swapIndex(m_corners[0], m_corners[1]);
}

if(m_corners[2]->getX() < m_corners[3]->getX()){
    swapIndex(m_corners[2], m_corners[3]);
}

This is the code that the values are correct after, the breakpoint is set on a scaffold line immediately below this and the method ends the line after that.

This is where it is being called and where the next breakpoint is and where the value is different:

sortCorners();
LOG(Status, "Successfully Sorted Corners");

The values in m_corners in the first breakpoint are: (171,381), (839, 416), (809, 1077), (171, 1077)

The values when returning the the other method changes each time but on the last run were: (-1176399856, 21917), (-1176237744, 21917), (0,0), (-1176399856, 21917). The 'Y' variables are always the same though and there is always a (0,0) point but sometimes the 'X' is a huge positive number instead, so my guess is it is overflowing but I don't know why.

This is the code to initialize m_corners. First in the .h file:`

Coordinate* m_corners[4]{};`

Then in the cpp file:

for (int i = 0; i < circles.size(); i++) {
        m_corners[i] = new Coordinate((int) circles[i][X], (int) circles[i][Y]);
    }

where circles is what is given from OpenCVs 'HoughCircles'

Aucun commentaire:

Enregistrer un commentaire