a small bug in ofImage::clear()

ok, so I found that if you clone an image, then clear it, and then try to clone again, the application crashes.

offending code:

  
  
void testApp::setup(){  
	bikers.loadImage("images/bikers.jpg");  
	gears.loadImage("images/gears.gif");  
	cloned = bikers;  
	cloned.clear();  
    cloned = gears;  
}  
  

I found that clear was not cleaning the underlying bmp, so you just have to add

  
  
bmp 					= NULL;  
  

to ofImage::clear()

so it ends up like this

  
  
void ofImage::clear(){  
	//-------------------------------  
	if(isValid() && bFreeImageInited) {  
		// zach - I added "if (bFreeImageInited)" since  
		// app runner might kill freeImage, so images killing themselves might not  
		// want to call anything freeImage related.  
		FreeImage_Unload(bmp);  
	}  
  
	//-------------------------------  
	if (imgPixels != NULL && bAllocatedPixels == true){  
  
		delete [] imgPixels;				// delete anything that might have existed  
		bAllocatedPixels = false;  
	}  
  
	//-------------------------------  
	bmp 					= NULL; //<---  
	width = height = bpp = 0;  
	type 					= OF_IMAGE_UNDEFINED;  
	bUseTexture 			= true;		// the default is, yes, use a texture  
	bAllocatedPixels		= false;  
	tex.clear();  
}  
  
  

hope it makes sense.
ciao!

Hi I’m not sure I’m writing this correctly but I cant seem to get the image clear() function to work when I run the following code I get a runtime crash:

In my .h:

#ifndef _TEST_APP
#define _TEST_APP

#include “ofMain.h”
#define numberImages 150

class testApp : public ofSimpleApp{

public:

ofImage sequence[numberImages];
int frame;
void setup();
void update();
void draw();

void keyPressed (int key);
void keyReleased (int key);

void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased();

};

#endif

in testapp.cpp:

  
  
#include "testApp.h"  
  
  
//--------------------------------------------------------------  
void testApp::setup(){  
  
  
    frame = 1;  
    for (int i = 0; i < numberImages; i++){  
    sequence[i].setUseTexture(true);  
    string myImageName = "Comp 1_" + ofToString(i) + ".png";  
    sequence[i].loadImage(myImageName);  
    }  
  
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
  
     if (frame < 50){  
     frame ++;  
     }  
     if (frame > 10){  
             sequence[1].clear();  
     }  
  
  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
  
        ofEnableAlphaBlending();  
        sequence[frame].draw(0, 0);  
  
}  

did you find a workaround?

i’m running into the same problem on osx 10.5.2 with oF 0.05. but not on 10.4.1.

the crash seems to be caused by FreeImage_Unload.

jeremy