Copying ofVideoGrabber pixels to an ofImage?

I’m building off the osxVideoRecorderExample, and I’m trying to grab pixel data from the video grabber and assign those pixels to an ofImage.

I’ve made a small frame buffer that creates a set of images in the constructor and places them in a vector. Then whenever the videograbber has a new image I update the current index in the buffer and also update the pixels in the corresponding image.

The problem I’m running into, though is that when I update the pixels in one of the images from the videograbber pixels, I get the following error:

osxVideoRecorderExampleDebug(17407,0xa16151a8) malloc: *** error for object 0xbffff568: pointer being freed was not allocated

My first thought was that I’m mixing up pointers and color data, but xcode should catch that, right? I’m new to c++ and OF.

Here’s some code:

xcode takes me to this section of code when it errors:

template<typename PixelType>
void ofPixels_<PixelType>::clear(){
    if(pixels){
	    if(pixelsOwner) delete[] pixels;
	    pixels = NULL;
    }

    width			= 0;
    height			= 0;
    channels		= 0;
	bAllocated		= false;
}

Here’s what I’ve changed in the example:

// instantiate frame buffer
testApp::testApp() : frameBuffer(10){}

testApp::update(){
    ofBackground(60, 60, 60);
    if (vidGrabber.isFrameNew()) {
        frameBuffer.insertFrameAsPixels(vidGrabber.getPixelsRef());
    }
    vidGrabber.update();
}

And here’s the relevant code from FrameBuffer

#include "FrameBuffer.h"

int numFrames = 0;
int currentFrameIndex = 0;
vector<ofImage *> frames;

FrameBuffer::FrameBuffer(int initNumFrames){

    numFrames = initNumFrames;
    int i = 0;
    while (i < numFrames){
        ofImage img;
        frames.push_back(&img);
        i++;
    }
};
int FrameBuffer::insertFrameAsPixels(ofPixels pixels){
    currentFrameIndex = nextFrame(currentFrameIndex);
    frames[currentFrameIndex]->setFromPixels(pixels);
    return currentFrameIndex;
};
int FrameBuffer::nextFrame(int currentFrameIndex){
    currentFrameIndex++;
    if (currentFrameIndex >= numFrames){
        return 0;
    } else {
        return currentFrameIndex;
    }
};

Here’s where things get even weirder. If I put a log into FrameBuffer::insertFrameAsPixels I DON’T get the malloc error.

int FrameBuffer::insertFrameAsPixels(ofPixels pixels){
    currentFrameIndex = nextFrame(currentFrameIndex);
    ofLog(OF_LOG_VERBOSE, "a log message"); // now it doesn't error
    frames[currentFrameIndex]->setFromPixels(pixels);
    return currentFrameIndex;
};

What am I doing wrong, or failing to grok in this example?

I would instantiate the FrameBuffer in testApp::setup. I have never actually seen the constructor for testApp used like that. It may be possible certain ofImage stuff is not available at that point.

Sound’s reasonable. Like I said, I’m new to c++ and xcode wanted me to construct it that way since I didn’t include a default constructor.

I updated the code so the constructor is empty, and moved that code to a FrameBuffer::setup() method, and added that call to the testApp::setup but the exact same thing is happening. Works with an ofLog, but errors without.

if you put

if(frames[currentFrameIndex])
{
ofLogVerbose() << currentFrameIndex << " EXISTS";
}else
{
ofLogVerbose() << currentFrameIndex << " DOES NOT EXIST";
}

Do you ever get “DOES NOT EXIST”? Just wondering if it could be a logic error.