我创建了一个 iOS 应用程序,在其中使用 GLKViews 来实现 OpenGL 环境。现在一切都按预期工作,我注意到一旦我重新实例化视图大约三倍,我就会收到大量内存分配和内存警告。在初始化时,我加载这样的纹理:
NSDictionary * options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES],
GLKTextureLoaderOriginBottomLeft,
nil];
NSError *error;
topTexture = [GLKTextureLoader textureWithCGImage:[self imageWithView:v].CGImage options:options error:&error];
if (error) {
NSLog(@"Error loading texture from image: %@",error);
}
并在 GLKView 像这样消失时删除它们(和缓冲区):
-(void)cleanUp{
if(vertexArray != 0){
glDeleteBuffers(1, &vertexArray);
vertexArray = 0;
}
if( texArray != 0){
glDeleteBuffers(1, &texArray);
texArray = 0;
}
GLuint fT = frontTexture.name;
glDeleteTextures(1, &fT);
GLuint lT = leftTexture.name;
glDeleteTextures(1, &lT);
GLuint rT = rightTexture.name;
glDeleteTextures(1, &rT);
GLuint baT = backTexture.name;
glDeleteTextures(1, &baT);
GLuint boT = bottomTexture.name;
glDeleteTextures(1, &boT);
GLuint tT = topTexture.name;
glDeleteTextures(1, &tT);
}
这就是它们的绘制方式:
-(void)draw{
[self.effect prepareToDraw];
self.effect.texture2d0.enabled = YES;
for(int i=0;i<6;i++){
if(i==0)glBindTexture(GL_TEXTURE_2D, frontTexture.name);
if(i==1)glBindTexture(GL_TEXTURE_2D, rightTexture.name);
if(i==2)glBindTexture(GL_TEXTURE_2D, backTexture.name);
if(i==3)glBindTexture(GL_TEXTURE_2D, leftTexture.name);
if(i==4)glBindTexture(GL_TEXTURE_2D, bottomTexture.name);
if(i==5)glBindTexture(GL_TEXTURE_2D, topTexture.name);
glDrawArrays(GL_TRIANGLES, i*6, 6);
}
}
尽管如此,当我观察分配时,实时字节在实例化时会增加 2 MB。然后,当我删除视图时,实时字节保持不变。因此,似乎创建的 2MB 从未被释放。