deleting new unsigned chars properly (memory leak with memcpy)

Hi guys - having some trouble with the below code… OSX activity monitor just shows the programme growing and growing whenever menuState==2 is hit. Presumably there’s something wrong with the way I’m deallocating camPixels? Any ideas?

if(menuState==2){

//vidGrabber.draw(0, 0);
if(!masterAnalysis.synthesisComplete){
//cout<<masterAnalysis.synthesisComplete<<“masterAnalysis.synthesisComplete \n”;
//cout<<“in draw loop menuState 2 \n”;
if (vidGrabber.isFrameNew())
{
camPixels = new unsigned char[camWidth*camHeight*3];
memcpy(camPixels, vidGrabber.getPixels(), (camWidth*camHeight*3));
//memcpy(someLocalPixels, pixels, (camWidth*camHeight*3));
masterAnalysis.synthDrawCamRecord(camPixels);
}

// the old way:
// masterAnalysis.synthDrawCamRecord(vidGrabber.getPixels());

} else {
menuState = 3;
delete [] camPixels;
}
}

it’s not a memcpy() problem, but a new/delete problem.

every time you say “new”, there should be a “delete”. if you’re working with an array, then “type* x = new type[];” should be followed by “delete [] x;”

in general you shouldn’t be using new and delete at all. in this case you can use ofPixels, which handles allocation for you.

instead of being an unsigned char*, make camPixels an ofPixels. then instead of memcpy you can just say camPixels = vidGrabber

if you need an unsigned char* for your synthDrawCamRecord, then you can get it with camPixels.getPixels().

ah ha - i had gotten my head all wrapped around chris’ memcpy example here (which is quite old now that i look) - http://forum.openframeworks.cc/t/recording-quicktime-with-sound-in-sync/775/0

thanks Kyle - will have a look at what you suggest - cheers! :slight_smile:

hi Kyle - thanks again!

just trying to figure out how to get vidGrabber to return ofPixels as you suggest - nearest I can figure requires something like this:

camPixels.setFromPixels(vidGrabber.getPixels(), camWidth, camHeight, 0x01);

where camPixels as an ofPixels. is there a simpler way around this, as you suggest (camPixels = vidGrabber)?

you can just do:

  
  
//.h  
ofPixels camPixels;  
  
//.cpp  
camPixels = vidGrabber.getPixelsRef();  
  

wow now there’s a sweet call. where’s the best place to look for info on ofPixels these days? any good examples?

thanks as always arturo and kyle!

no there’s no examples or reference yet, you can take a look at ofPixels.h to see what methods are available and there’s also this doxygen docs:

http://www.undef.ch/uploads/ofDoc/html/classof-pixels--.html

ah yes cool - thanks! while i’ve got your ‘ear’, a somewhat related issue…

all this is headed towards an app that grabs frames from a camera while displaying simple animations to the screen - all happening currently in: masterAnalysis.synthDrawCamRecord(camPixels);

it’s a bit like a structured light application - doing some “shape from shadow” and “shape from shading” using a set of test patterns on the screen, where we need to know the exact thing that’s on the screen when an image from the camera comes in. so it’s a sync issue between the camera and the on-screen writing.

when i pass ofPixels the frame saving operations in masterAnalysis.synthDrawCamRecord() block the drawing operation, compared to the unsigned char * method (which I couldn’t get to work properly for other reasons - mem leaks, etc.).

I suppose the question is leading to: Is the best way to record a sync’d frame from a camera while animating to the screen to use threading? That is, start a frame thread and then just ‘tag’ the saved images based on what’s on the screen when the image is taken?

(E.g.: Memo’s multithread image saver: http://forum.openframeworks.cc/t/multithreaded-image-saver/1687/0)

the unsigned char* method and the = operator in ofPixels are doing the same, there shouldn’t be any difference. that said, yes if your application main thread is very time consuming then probably you’ll have better results using a thread to save the images