How to pass unsigned char* array to image?

I want to pass the unsigned char* array ‘buffer’ (frame buffer) into an image,so that I can have an image that contains frames of the video grabber,
here is my code


when I run it,it can build succeeded but in ‘ofImage.cpp’there is an error"Thread 1:EXC_BAD_ACCESS’ imediately as below:

Seems that width and height variables are not defined.
If your’re looking for the processing analog use getWidth() and getHeight().

width and height are defined, and I tried getWidth() and getHeight(),but still doesn’t work

even draw the first image would cause the exc_bad_access

I’m guessing you are trying to use an array of ofImages,if so you must define its length first. If you need a flexible container use a vector.

This is from the grabber example

ofApp.h
ofImage images[1];

ofApp.cpp
void ofApp::setup(){
camWidth = 320; // try to grab at this size.
camHeight = 240;

    //we can now get back a list of devices.
    vector<ofVideoDevice> devices = vidGrabber.listDevices();

    for(int i = 0; i < devices.size(); i++){
        if(devices[i].bAvailable){
            ofLogNotice() << devices[i].id << ": " << devices[i].deviceName;
        }else{
            ofLogNotice() << devices[i].id << ": " << devices[i].deviceName << " - unavailable ";
        }
    }

    vidGrabber.setDeviceID(2);
    vidGrabber.setDesiredFrameRate(60);
    vidGrabber.initGrabber(camWidth, camHeight);

    videoInverted.allocate(camWidth, camHeight, OF_PIXELS_RGB);
    videoTexture.allocate(videoInverted);
    ofSetVerticalSync(true);

	image.allocate(camWidth, camHeight, OF_IMAGE_COLOR);
	images[0].allocate(camWidth, camHeight, OF_IMAGE_COLOR);
}


//--------------------------------------------------------------
void ofApp::update(){
    ofBackground(100, 100, 100);
    vidGrabber.update();

    if(vidGrabber.isFrameNew()){
        ofPixels & pixels = vidGrabber.getPixels();
        for(int i = 0; i < pixels.size(); i++){
            videoInverted[i] = 255 - pixels[i];
        }
        videoTexture.loadData(videoInverted);
		image.setFromPixels(pixels);
		images[0].setFromPixels(pixels);
    }
}

//--------------------------------------------------------------
void ofApp::draw(){
    ofSetHexColor(0xffffff);
    vidGrabber.draw(20, 20);
    videoTexture.draw(20 + camWidth, 20, camWidth, camHeight);
	image.draw(2*(20 + camWidth), 20, camWidth, camHeight);
	images[0].draw(2 * (20 + camWidth), 20 + camHeight, camWidth, camHeight);
}

are videoInverted and videoTexture both ofImage?

ofPixels videoInverted;
ofTexture videoTexture;

See the code in of_v0.9.8_vs_release\examples\video\videoGrabberExample

You can use onde of these types. ofImage is a larger object with more features like
ofPixels and ofTexture members for allocating pixels and drawing them to screen.

If you are not loading external images better use ofPixels or ofTexture.