title: Avoiding memory leaks when storing OpenCV Mat objects in STL containers
body: <p>I'm using OpenCV and I want to store a number of images (<code>Mat</code> objects) in a vector. I have declared the vector as follows to store pointers to <code>Mat</code> objects.</p>
  
  <pre><code>std::vector&lt;Mat*&gt; images;
  </code></pre>
  
  <p>The <code>Mat</code> objects are created using the <code>new</code> keyword, and then added to the vector.</p>
  
  <pre><code>Mat *img = new Mat(height, width, CV_8UC3);
  // set the values for the pixels here
  images.push_back(img);
  </code></pre>
  
  <p>How would I go about making sure that I free the memory occupied by the <code>Mat</code> objects to avoid memory leaks?</p>
  
  <p>What I'm doing right now is the following:</p>
  
  <pre><code>Mat *im = images.at(index);
  // process and display image here
  delete(im);
  </code></pre>
  
  <p>Valgrind is reporing a possible memory leak with reference to the created Mat objects. Am I missing something?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Ok. Apparently it is better to avoid using <code>Mat</code> pointers and dynamically allocating <code>Mat</code> using <code>new</code>. I have modified my code to use <code>std::vector&lt;Mat&gt;</code> instead. However, I still see some blocks that were allocated by <code>Mat</code> possibly lost in the Valgrind report. I also notice that memory usage steadily increases while the program is running.</p>
  
  <p>Let me clarify what I'm doing. I'm creating images in a function and placing them in a buffer (internally using a <code>std::deque</code>). This buffer is then accessed by another function to retrieve and image and pass it to another function which performs processing and rendering. </p>
  
  <pre><code>class Render {
     public:
        void setImage(Mat&amp; img) {
           this-&gt;image = img;
        }
  
        void render() {
           // process image and render here
        }
     private:
        Mat image;
  }
  </code></pre>
  
  <p>Thread that continuously fetches images from buffer and renders them.</p>
  
  <pre><code>void* process(void *opaque) {
  
     ImageProcessor *imgProc = (ImageProcessor*) opaque;
     Mat img;
  
  
     while (imgProc-&gt;isRunning) {
        // get an image from the buffer
        imgProc-&gt;buffer-&gt;getFront(img);
  
        // set the image
        imgProc-&gt;renderer-&gt;setImage(img);
  
        // process and render
        imgProc-&gt;renderer-&gt;render();
     }
  
  }
  </code></pre>
  
  <p>Now, everything is passed as object references (i.e. <code>Mat&amp;</code>). I assume that after getting an image from the buffer and passing it to the rendering function, the only reference to that object will be in that function. Therefore, when I get another image, there will no longer be a reference to that object and it will be destroyed. But Valgrind gives me the following:</p>
  
  <pre><code>25,952,564 bytes in 11 blocks are possibly lost in loss record 14,852 of 14,853
    in ImageProcessor::generateImage() in ImageProcessor.cpp:393
    1: malloc in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so
    2: cv::fastMalloc(unsigned long) in /usr/local/lib/libopencv_core.so.2.4.2
    3: cv::Mat::create(int, int const*, int) in /usr/local/lib/libopencv_core.so.2.4.2
    4: cv::Mat::create(int, int, int) in /usr/local/include/opencv2/core/mat.hpp:353
    5: cv::Mat::Mat(int, int, int) in /usr/local/include/opencv2/core/mat.hpp:75
    ...
  </code></pre>
  
  <p>And here is <code>generateImage()</code>:</p>
  
  <pre><code>void generateImage() {
     Mat img(h, w, CV_8UC3); 
     // set values of pixels here
     this-&gt;buffer-&gt;pushBack(img);
  }
  </code></pre>