我的程序一次将大型 jpeg 渲染为纹理,并允许用户通过重新使用“相同”纹理在它们之间切换。
我在开始时调用了一次 glGenTextures(1, &texture);
。
然后每次我想交换我使用的图像时:
FreeTexture( texture );
ROI_img = fetch_image(temp, sortVector[tPiece]);
loadTexture_Ipl( ROI_img , &texture );
这里是调用的两个函数:
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->imageData);
return 0;
}
void FreeTexture(GLuint texture)
{
glDeleteTextures( 1, &texture);
}
我的问题是,在几个图像之后它们停止渲染(纹理全黑)。如果我继续尝试切换,我会收到此错误消息:
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] <Error>: CGSImageDataLock: Cannot allocate memory
我以为 glDeleteTextures
每次都会释放内存??
关于如何更好地实现它有什么想法吗?
附注这是遇到内存泄漏的屏幕 (https://p.twimg.com/AoWVy8FCMAEK8q7.png:large)
最佳答案
既然你想重用你的纹理,你不应该用 glDeleteTextures 删除它。如果这样做,您需要使用 glGenTextures 创建一个新纹理,我没有看到您在 loadTexture_Ipl 函数中这样做。
关于c++ - OpenGL glDeleteTextures 导致内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9770063/