1
我創建了一個iOS應用程序,我使用GLKViews來實現OpenGL環境。現在一切都按預期工作,我注意到一旦我重新實例化了三次以上的視圖,就會得到巨大的內存分配和內存警告。在初始化時我加載紋理是這樣的: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);
}
}
不過,當我觀察分配時,活動字節在實例化時增加了2MB。然後,當我刪除視圖時,活動字節保持不變。所以似乎創建的2MB從未發佈。