How to debug "exc_bad_access" at ofRectangle?

New to OF. I stuck at “bad_access” problem. Any can give me a clue how I debug it?

I put a button in draw() but alway get “bad_access” at
"
void ofRectangle::set(float px, float py, float w, float h) {
x = px;
y = py; // exc_bad_access(code=1, address 0x0)
width = w;
height = h;
}
"

Here is how i draw the button:

int marginX = ofGetWidth() * 0.1;
int marginY = ofGetHeight() * 0.1;

// record button
recordButton = ofRectangle(marginX, marginY, ofGetWidth() * 0.8, ofGetWidth() * 0.8);

ofSetColor(255,0,0);
if (bIsPressing) {
    ofNoFill();
}
ofRect(recordButton);
ofFill();

Thank so much!

can you post more of your code or the smallest possible example that shows the crash?

Thanks.
It works well now, Not sure why. What I did is just : comment other parts of the code until only left the button…and bad access still happens.And then I close the file and reopen it. The crash never happen again. I put back other part of the code, works well.

Wired. So I still past my mess code here:

//--------------------------------------------------------------
void ofApp::setup(){
    
    ofBackground(0,0,0);
    
    w = 320;
    h = 240;
    
    movie.initGrabber(w, h, true);
    
    //reserve memory for cv images
    rgb.allocate(w, h);
    hsb.allocate(w, h);
    hue.allocate(w, h);
    sat.allocate(w, h);
    bri.allocate(w, h);
    filtered.allocate(w, h);
    
    //osc
    sender.setup(HOST, PORT);
    
    uIsRecord= true;
    uIsPlay=false;
    bIsPressing=false;


}

//--------------------------------------------------------------
void ofApp::update(){
    
    movie.update();
    
    if (movie.isFrameNew()) {
        
        //copy webcam pixels to rgb image
        rgb.setFromPixels(movie.getPixels(), w, h);
        
        //mirror horizontal
        rgb.mirror(false, true);
        
        //duplicate rgb
        hsb = rgb;
        
        //convert to hsb
        hsb.convertRgbToHsv();
        
        //store the three channels as grayscale images
        hsb.convertToGrayscalePlanarImages(hue, sat, bri);
        
        //filter image based on the hue value were looking for
        for (int i=0; i<w*h; i++) {
            filtered.getPixels()[i] = ofInRange(hue.getPixels()[i],findHue-5,findHue+5) ? 255 : 0;
        }
        filtered.flagImageChanged();
        
        //run the contour finder on the filtered image to find blobs with a certain hue
        contours.findContours(filtered, 500, w*h/2, 1, false);
        std:ofSort(contours.blobs,my_compare);//sort bolbs by x position
        

        
    }
    
    
    
    
    

}

//--------------------------------------------------------------
void ofApp::draw(){

    if(uIsRecord)
    {
        drawRecord();

            }
    else if (uIsPlay)
    {
        drawPlay();
    
    }

}

void ofApp::drawRecord(){
    // show camera view
    
    ofSetColor(255,255,255);
    
    //draw all cv images
    rgb.draw(0,0);
    filtered.draw(320,0);
    contours.draw(320,0);
    
    
    
    // recording button
    ofSetColor(255, 0, 0);
    ofFill();
    int marginX = ofGetWidth() * 0.4;
    int marginY = ofGetHeight() * 0.5;
    
    
    recordButton = ofRectangle(marginX, marginY, ofGetWidth() * 0.15, ofGetWidth() * 0.05);
    
    ofSetColor(200,0,0);
    if (bIsPressing) {
        ofNoFill();
    }
    ofRect(recordButton);
    ofFill();
    
    
    
    
    //draw red circles for found blobs
    
    ofxOscMessage m;
    m.setAddress("/test");
    
    for (int i=0; i<contours.nBlobs; i++) {
        
        ofCircle(contours.blobs[i].centroid.x, contours.blobs[i].centroid.y, 20);
        ofDrawBitmapString ( "i ="  + ofToString (i, 1), 4, 10*i+80);
        ofDrawBitmapString ( "x ="  + ofToString (contours.blobs[i].boundingRect.x, 5), 64, 10*i+80);
        ofDrawBitmapString ( "y ="  + ofToString (contours.blobs[i].boundingRect.y, 5), 264, 10*i+80);
        
        
        
        bool flag=true;
        bool flag2=true;
        bool flag3=true;
        
        
        int leftx=contours.blobs[0].boundingRect.x;
        int rightx=contours.blobs[0].boundingRect.x+contours.blobs[0].boundingRect.width;
        
        int point1x=leftx+ (rightx-leftx)/6;
        int point2x=leftx+ (rightx-leftx)/2;
        int point3x=leftx+ (rightx-leftx)/6*5;
        
        
        for (int index=0; index <contours.blobs[0].nPts; index++)
        {
            
            if ((contours.blobs[0].pts[index].x >= point1x)&&(contours.blobs[0].pts[index].x <= point1x+5)&&(flag==true))
            { ofDrawBitmapString("minx[" + ofToString (contours.blobs[0].pts[index].x) + ofToString (contours.blobs[0].pts[index].y) + "]", 500, 200);
                flag=false;
            }
            
            if ((contours.blobs[0].pts[index].x >= point2x)&&(contours.blobs[0].pts[index].x <= point2x+5)&&(flag2==true))
            { ofDrawBitmapString("minx[" + ofToString (contours.blobs[0].pts[index].x) + ofToString (contours.blobs[0].pts[index].y) + "]", 500, 250);
                flag2=false;
            }
            if ((contours.blobs[0].pts[index].x >= point3x)&&(contours.blobs[0].pts[index].x <= point3x+5)&&(flag3==true))
            { ofDrawBitmapString("minx[" + ofToString (contours.blobs[0].pts[index].x) + ofToString (contours.blobs[0].pts[index].y) + "]", 500, 300);
                flag3=false;
            }
        }
        
        
        
        int Xmessage = contours.blobs[i].boundingRect.x;
        int Ymessage = contours.blobs[i].boundingRect.y;
        int Wmessage = contours.blobs[i].boundingRect.width;
        m.addIntArg(Xmessage);
        m.addIntArg(Ymessage);
        m.addIntArg(Wmessage);
        
        
        
    }
    
    
    
    sender.sendMessage(m);

    
    
    
    
    
    }

void ofApp::drawPlay(){


}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
    if(key == 'a' || key == 'A'){
        
        
    }
    

}

//--------------------------------------------------------------
void ofApp::keyReleased(int key){

}

//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){

}

//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){

}

//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
    
    //calculate local mouse x,y in image
    int mx = x % w;
    int my = y % h;
    
    //get hue value on mouse position
    if ((x>0)&&(x<320)&&(y>0)&&(y<240))
    {findHue = hue.getPixels()[my*w+mx];}
    
    
    if (recordButton.inside(x, y)) {
        bIsPressing = true;}

}

//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){
   
    if (uIsRecord){
    
        bIsPressing=false;
    
    }

}

//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){

}

//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){

}

//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){ 

}

HI, there’s no need to set the ofRectangle params every time you draw. Just set it up once in the setup() method. If you are worried about how resizing the window might affect your button, set the ofRectangle in the windowResized method.
Besides that it is better if you set your button parameters using the set method that copying using the = operator.
should be like this

recordButton.set(marginX, marginY, ofGetWidth() * 0.15, ofGetWidth() * 0.05);

best

i think ive seen this crash too, but it was a weird situation. im modifying the livecoding idea from here:

to do live looping as well, so i am saving/sending streams of input data and a big chunk of memory cold casted to gamestate.

the first time i send the chunk of memory over to the hot reloaded dll, i am using calloc to make sure eveything starts at zero. maybe i am setting a reference/pointer to zero or something? not sure.

my solution was just to take ofRectangle out of the loop state so it never gets cleared out to zero. i can’t save it my loops but at least it wont crash.

i think these are 2 different problems. in any case @racarate your problem seems to be related to clearing everything to 0. ofRectangle’s x and y are references to the internal x and y of the position ofPoint in ofRectangle. so if you initialize them to 0 they’ll become null pointers and you get a crash trying to access them. the fact that we have references is not a very good practice in my opinion and should probably be replaced with a union or similar

1 Like