How to properly clear vector<ofImage>


#1

Hi, I wonder how to properly release ofImage from memory when images are no longer being used.

For a single image, I think image.clear() is how to clear an image.

But if I have multiple images in vector, should I clear all the images separately like

    for (int i=0; i<images.size(); ++i)
        images[i].clear();

Or is simply clearing the vector good enough?

images.clear();

#2

if it’s a vector of ofImages, than clear() on the vector should clear the images since when the object is destroyed, clear is called and vector::clear() calls each objects destructor. The ofImage destructor calls clear:

(and in most cases OF objects are fairly smart in terms of clearing themselves, freeing up memory or textures, etc)

but if it’s a vector of ofImage pointers, ie, vector < ofImage * > images you will want to to clear the images directly with a for loop (similar to what you’ve written but for pointers) before clearing the vector.


#3

Thank you Zach :slight_smile: