Screengrab and pointer error


#1

Hi! sorry if this question has been already asked (I searched around and I didn’t seem to find it)

I am taking a screen capture on a keystroke, but as soon as I catch the image it gives me a pointer error:

pointer being freed was not allocated

I can’t seem to find how come, as the code is practically copied by an example, with only the variable name changed.

Here is the code:

.h

#pragma once

#include "ofMain.h"
#include "ofxCv.h"
#include "ofxFaceTracker.h"

class ofApp : public ofBaseApp {
public:
	void setup();
	void update();
	void draw();
	void keyPressed(int key);
	
	ofVideoGrabber cam;
	ofxFaceTracker tracker;
	ofEasyCam easyCam;
    ofImage previousImage;
};




.cpp

#include "ofApp.h"

using namespace ofxCv;

void ofApp::setup() {
	ofSetVerticalSync(true);
	cam.setup(640, 480);
	tracker.setup();
    ofBackground(0,0,0);
    ofSetBackgroundAuto(false);
}

void ofApp::update() {
	cam.update();
	if(cam.isFrameNew()) {
		tracker.update(toCv(cam));
	}
}

void ofApp::draw() {
	ofSetColor(255);
	//cam.draw(0, 0);
	//ofDrawBitmapString(ofToString((int) ofGetFrameRate()), 10, 20);

	if(tracker.getFound()) {
		//tracker.draw();
		ofMesh objectMesh = tracker.getObjectMesh();
		ofMesh meanMesh = tracker.getMeanObjectMesh();
        ofSetupScreenOrtho(640, 480, -1000, 1000);
        ofTranslate(320, 240);
        ofScale(5,5,5);
        cam.getTexture().bind();
        meanMesh.draw();
        cam.getTexture().unbind();
    }
    
}

void ofApp::keyPressed(int key) {
	if(key == 'r') {
		tracker.reset();
    } else if(key == 'x'){
        previousImage.grabScreen(0,0,ofGetWidth(),ofGetHeight());
        previousImage.save("screenshot.png");
    }
}

thank you for helping!
s


#2

Hello Salvatore. Try to add:

previousImage.allocate(ofGetWidth(), ofGetHeight(),  OF_IMAGE_COLOR);

In your setup method.


#3

@xdxdvsxdx which version of openFrameworks are you using?

This shouldn’t matter as it is done internally anyways.

Does it make any difference if you change

 previousImage.grabScreen(0,0,ofGetWidth(),ofGetHeight());
        previousImage.save("screenshot.png");

for

        ofSaveScreen("screenshot.png");

?


#4

hi everyone!
thanks for helping out!
the problem was fixed when I added an allocate on the image in the setup method, as suggested by @edapx
I didn’t understand the documentation, as it suggested that it “allocates memory”, and thus I thought that this step wasn’t needed.
Thanks so much to you all!
s


#5

good to know that it got fixed.
It is strange, though, as it should be handled internally. which version of OF are you using?


#6

I have this one:

of_v20180924_osx_release

thanks!
s