Mosaic Project Gives Errors... but not always? [video included]

Hello! My project is taking a live video, looking at and storing the xth and yth pixel color in a 2d array, then generating a pixelized version with squares.

I update each frame like this:

void ofApp::update(){
    capture.update(); // update with new frame
    
    ofPixels pixel = capture.getPixels();
    
    if (capture.isFrameNew()) {
        ofColor color;
        
        //in a 2D way, get a color in each gridsized pixel
        for (int x = 0; x < ofGetWidth()-gridSize; x += gridSize) {
            for (int y = 0; y < ofGetHeight()-gridSize; y += gridSize) {
                //assign color
                color = pixel.getColor(x, y);
                //save the color in the 2D array [makes it easier for squares]
                colors[x][y] = color;
            }
        }
    }
}

to mesh well with the square drawing :

 for (int x = 0; x < ofGetWidth(); x += gridSize) {
        // step through vertically
        for (int y = 0; y < ofGetHeight(); y += gridSize) {
            //can't set them at x, x is only the width and this is 2D
            ofSetColor(colors[x][y]);
            ofDrawRectangle(x, y, gridSize, gridSize);
        }
    }

The problem is, I am occasionally getting an EXC_BAD_ACCESS error, and I don’t understand what is causing it?

As you can see, the project occasionally crashes immediately, but sometimes it is fine with running and adjusting the grid. At first I thought that it was some sort of array overflow error, but it doesn’t seem like that would not trigger at any point.

I think that your problem is that sometimes x or y is outside the video size, i will do

for (int x = 0; x < capture.getWidth(); x += gridSize) {
            for (int y = 0; y <capture.getHeight(); y += gridSize) {

And you have to be sure that video width and height is divisible by gridSize

Anyway when it crash will show the line where the problem is, seems in update getColor, you can try to cout x and y positions to console to see if that is the problem

Thank you!

I’ve just tried using the cout, and it doesn’t actually look like my x and y go outside the boundaries. (I’ve also changed it so my gridsize is now always easily dividable, thank you)The last two before the crash were x: 0, y: 696. (dimensions are 1024 x 768)
When it runs successfully, the last two before ‘redraw’ are x: 984, y: 720.

So still a bit confused as to what is happening.

When you get the crash you can check i wich line is crashing, doing click in the left column

Which line is pointing the error? and in this point you can check the values of the x and y