title: C++ Memory leaks on Windows 7
body: <p>I'm writing a program (C++, MinGW 32 bit) to batch process images using OpenCV functions, using AngelScript as a scripting language. As of right now, my software has some memory leaks that add up pretty quickly (the images are 100-200 MB each, and I'm processing thousands at once) but I'm running into an image where Windows doesn't seem to be releasing the memory used by my program until rebooting.</p>
  
  <p>If I run it on a large set of images, it runs for a while and eventually OpenCV throws an exception saying that it's out of memory. At that point, I close the program, and Task Manager's physical memory meter drops back down to where it was before I started. But here's the catch - every time I try to run the program again, it will fail right off the bat to allocate memory to OpenCV, until I reboot the computer, at which point it will work just great for a few hundred images again.</p>
  
  <p>Is there some way Windows could be holding on to that memory? Or is there another reason why Windows would fail to allocate memory to my program until a reboot occurs? This doesn't make sense to me.</p>
  
  <p><strong>EDIT:</strong> The computer I'm running this program on is Windows 7 64 bit with 32 GB of ram, so even with my program's memory issues, it's only using a small amount of the available memory. Normally the program maxes out at a little over 1 GB of ram before it quits.</p>
  
  <p><strong>EDIT 2:</strong> I'm also using FreeImage to load the images, I forgot to mention that. Here's the basis of my processing code:</p>
  
  <pre><code>//load bitmap with FreeImage
  FIBITMAP *bitmap = NULL;
  FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
  fif = FreeImage_GetFileType(filename.c_str(), 0);
  bitmap = FreeImage_Load(fif, filename.c_str(), 0);
  if (!bitmap) {
      LogString("ScriptEngine: input file is not readable.");
      processingFile = false;
      return false;
  }
  //convert FreeImage bitmap to my custom wrapper for OpenCV::Mat
  ScriptImage img;
  img.image = fi2cv(bitmap);
  FreeImage_Unload(bitmap);
  
  try {
      //this executes the AngelScript code
      r = ctx-&gt;Execute();
  } catch(std::exception e) {
      std::cout &lt;&lt; "Exception in " &lt;&lt; __FILE__ &lt;&lt; ", line " &lt;&lt; __LINE__ &lt;&lt; ", " &lt;&lt; __FUNCTION__ &lt;&lt; ": " &lt;&lt; e.what() &lt;&lt; std::endl;
  }
  
  try {
      engine-&gt;GarbageCollect(asGC_FULL_CYCLE | asGC_DESTROY_GARBAGE);
  } catch (std::exception e) {
      std::cout &lt;&lt; "Exception in " &lt;&lt; __FILE__ &lt;&lt; ", line " &lt;&lt; __LINE__ &lt;&lt; ", " &lt;&lt; __FUNCTION__ &lt;&lt; ": " &lt;&lt; e.what() &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>As you can see, the only pointer is to the FIBITMAP, which is freed.</p>