ofImage.setFromPixels 0.03 Windows memory leak

For this function I have a memory leak of 14kb per frame. I can’t work it out. Here is some simple test code:

// main.cpp  
int main( ){  
	ofSetupOpenGL(640,480, OF_WINDOW);	  
	ofRunApp(new testApp());  
void testApp::setup(){  
	pix = new unsigned char[320*240*3];  
	for (int i = 0; i < 320*240*3; i++){  
		pix[i] = 255;  
	testImage.allocate(320,240, OF_IMAGE_COLOR);  
void testApp::update(){  
	testImage.setFromPixels(pix, 320, 240, OF_IMAGE_COLOR);  
void testApp::draw(){  

I can’t see anything in setFromPixels that would do this. I even simplified the windows code to this, and it still does it…

			int bytesPerPixel = bpp / 8;  
			IMG->bmp = FreeImage_ConvertFromRawBits(pixels, w,h, w*bytesPerPixel, bpp, 0,0,0, true);  

I have the same problem if I use OF_IMAGE_GRAYSCALE (not converting, but from the allocate and setpixels) , although the white image doesnt appear which is even more strange.

Any thoughts?

hi chris,

it’s a memory leak - I’m sorry about that. we figured something wrong about freeImage…

you can add this to the top

void ofImage::setFromPixels(unsigned char * pixels, int w, int h, int newType){  
	if(IMG->isValid()) {  
	if (IMG->pixels != NULL && bAllocatedPixels == true){  
		delete IMG->pixels;				// delete anything that might have existed  

that’s a temporary fix which isn’t the smart thing if you are, for example uploading images always the same size… I will look into a smarter fix.

please let me know if that helps & apologies…

take care -

Oh darn. All I need to do is save an image every so often, but can’t do a screengrab, needs to be pixel uploaded.

Does it matter if I upload an image the same size each time using this hack? What would happen?


hi chris,

I think this solution will work, it’s just not optimal. I will work on an optimal solution (ie, save time deallocating and reallocating pixels[] if the image being uploaded is the same size/type) but for now, this solution should work fine for what you want to do.

sorry about the issue - we are on it. thanks for pointing it out.

take care!

Hi Zach

Did we ever solve this memory leak with setFromPixels for ofImage ?

I used 0.04 for this project and the bug still seems to be there.


95 % sure it’s gotten, but the 5% of me is checking now –

I’ve been testing this more, i think the memory leak is definitely in 0.04 for setFromPixels. However, there doesn’t seem to be a memory leak in 005.

I can’t updgrade my project to 005 as need to keep it 004 for now. Can I just copy all of the ofImage sources from 005 to 004 without problems?

yes you should be able to do this without problem… we’ve cleaned up ofImage alot in 0.05. We also cleaned up ofTexture too, so if you need to, you can also bring that over.

take care!