title: OpenGL glDeleteTextures leading to memory leak?
body: <p>My program renders large jpegs as textures one at a time and allows the user to switch between them by re-using the "same" texture.</p>
  
  <p>I call <code>glGenTextures(1, &amp;texture);</code> once at the start.</p>
  
  <p>Then each time I want to swap the image I use:</p>
  
  <pre><code>FreeTexture( texture );
  ROI_img = fetch_image(temp, sortVector[tPiece]);
  
  loadTexture_Ipl( ROI_img , &amp;texture );
  </code></pre>
  
  <p>here are the two functions being called:</p>
  
  <pre><code>int loadTexture_Ipl(IplImage *image, GLuint *text)
  {
      if (image==NULL) return -1;
      glBindTexture( GL_TEXTURE_2D, *text );
  
      glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
      glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
      glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
  
      glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, ROI_WIDTH, ROI_HEIGHT,0, GL_BGR, GL_UNSIGNED_BYTE, image-&gt;imageData);
      return 0;
  }
  
  void FreeTexture(GLuint texture)
  {
      glDeleteTextures( 1, &amp;texture);
  }
  </code></pre>
  
  <p>My problem is that after a couple of images they stop rendering (texture is all black). If I keep trying to switch I get this error message:</p>
  
  <pre><code>test(55248,0xacdf22c0) malloc: *** mmap(size=744001536) failed (error code=12)
  *** error: can't allocate region
  *** set a breakpoint in malloc_error_break to debug
  Mar 19 11:57:51 Ants-MacBook-Pro.local test[55248] &lt;Error&gt;: CGSImageDataLock: Cannot allocate memory
  </code></pre>
  
  <p>I thought that <code>glDeleteTextures</code> would free the memory each time??</p>
  
  <p>Any ideas on how to better implement this? </p>
  
  <p>p.s. here is a screen of the memory leaks being encountered (https://p.twimg.com/AoWVy8FCMAEK8q7.png:large)</p>