Why is my memory footprint growing?

I use ofxThreadedImageLoader to dynamically load images. And I use another thread to unload/delete. I have condensed my problem into a little piece of code. I don’t need the delete-thread in this example but I do need it in my real program, so that’s why it’s in here.

That’s the testApp.cpp:

  
void testApp::setup(){	  
    alreadyLoaded = false;  
    tmpImage = new ofImage();  
      
    loader.loadFromDisk(tmpImage, "images/test.png");  
    loader.startThread(true, false);  
    imageDeleter.start();  
      
}  
  
  
void testApp::update(){  
    bool alreadyLoaded = tmpImage->isUsingTexture();  
      
    if(alreadyLoaded) {  
        ofTexture& tex = tmpImage->getTextureReference();  
        alreadyLoaded = tex.bAllocated();  
    }  
      
    if(alreadyLoaded) {   
        imageDeleter.addImageToDelete(tmpImage);  
  
        tmpImage = new ofImage();  
        int nImages = DIR.listDir("images/");  
          
        loader.loadFromDisk(tmpImage, DIR.getPath((int) ofRandom(nImages)));  
    }  
}  

And that’s the threaded delete:

  
class threadedDelete : public ofxThread{  
      
public:  
    deque<ofImage *> images_to_delete;  
      
    //--------------------------  
    threadedDelete() {  
          
    }  
      
      
    threadedDelete(ofImage*  _imageToDelete){  
        lock();  
        images_to_delete.push_back(_imageToDelete);  
        unlock();  
    }  
      
      
    void addImageToDelete(ofImage*  _imageToDelete) {  
        lock();  
        images_to_delete.push_back(_imageToDelete);  
        unlock();  
    }  
      
    void start(){  
        startThread(true, false);   // blocking, verbose  
    }  
      
    void stop(){  
        stopThread();  
    }  
      
    //--------------------------  
    void threadedFunction(){  
        while(isThreadRunning()){  
            if (images_to_delete.size() > 0) {  
                lock();  
  
                ofImage  * imageToDelete(images_to_delete.front());  
                ofLog(OF_LOG_NOTICE, "deleting " + ofToString(imageToDelete));   
                images_to_delete.pop_front();  
                delete imageToDelete;  
  
                unlock();  
            }  
            else {  
                ofSleepMillis(50);  
            }  
        }  
    }  
      
};  

So basically I’m just loading a random image and when it’s loaded, I delete it and load another one. Even though every new ofImage gets deleted, my memory footprint grows until I get a memory warning (iOS) and my app crashes. What do I miss?

line 39 looks the most suspicious (actually surprised it works)

  
ofImage  * imageToDelete(images_to_delete.front());    

maybe ofImage is copying it here?

i think jvcleave is right - try

  
ofImage  * imageToDelete = images_to_delete.front();  

also if you’re on OSX try using Instruments to track down the leak. on Linux you can use oprofile, it’s fiddly to set up but great once it works.

huh, oprofile looks pretty useful, nice tip!