title: OpenCV - closing the image display window
body: <p>I am doing on a project for searching through an image database, and when I find the results to some query - 5 database images, I would like to display the results visually. I do not keep all the images in memory, so I have do load the image first in order to display it.</p>
  
  <p>I had something simple in mind, in pseudocode:</p>
  
  <pre><code>for image 1..5
      load images
      display image in a window
      wait for any keypress
      close the window
  </code></pre>
  
  <p>Here's a snippet of my code in <code>C++</code> using <code>OpenCV</code> for this purpose:</p>
  
  <pre><code>IplImage *img;
  
  for (int i=0; i &lt; 5; ++i){
      img = cvLoadImage(images[i].name.c_str(),1);
      cvShowImage(("Match" + images[i].name).c_str(), img);
      cvWaitKey(0);
      cvDestroyWindow(("Match" + images[i].name).c_str());
      // sleep(1);
      cvReleaseImage(&amp;img);
  }
  </code></pre>
  
  <p>The <code>images</code> array used here does not as such exist in my code, but for the sake of the question, it contains the File Names of the images relative to the current program running point if its <code>name</code> member. I store the image names a bit differently in my project.</p>
  
  <p>The code above <em>almost</em> works: I can iterate through 4/5 images OK, but when last image is displayed and a key is pressed, the image goes gray and I can not close the image window withouth crashing the rest of my application.</p>
  
  <p>My first idea was that becouse of compile-time optimizations, <code>cvReleaseImage</code> releases the image before <code>cvDestroyWindow</code> is finished, and that somehow makes it freeze. But, I've tried adding some waiting time (hence the commented out <code>sleep(1)</code> line of my code) and it didn't help.</p>
  
  <p>I am calling this display functionality from my console application, and when the image freezes, the control returns back to my application and I can keep using it (but the image window is still frozen in the background).</p>
  
  <p>Can you give me any suggestions on how to fix this?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I have talked to some people dealing with computer vision and OpenCV on a regular basis since asking the question, and still no ideas.</p>
  
  <p>I have also found a similar <a href="https://stackoverflow.com/q/7139968/884412">stackoverflow question</a>, but there is still no accepted answer. Googleing just gives similar questions as a result, but no answers.</p>
  
  <p>Any ideas on what to try (even if they are not the complete solution) are very much appreciated. </p>