現在、NSView から OpenGL テクスチャを取得するコードを書いています。コードの大部分は Apple のサンプル コードです。ただし、C++ クラスで使用する必要があり、オブジェクトのリークに問題があります。
コードの興味深い部分は次のとおりです。
GLuint CPlusPlusClass::openGLTexFromNSView(NSView* theView)
{
GLuint texName = 0x0;
@autoreleasepool // 1
{ // 1
NSBitmapImageRep* bitmap = [theView bitmapImageRepForCachingDisplayInRect:[theView visibleRect]];
int samplesPerPixel = 0;
[theView cacheDisplayInRect:[theView visibleRect] toBitmapImageRep:bitmap];
samplesPerPixel = (int)[bitmap samplesPerPixel];
glPixelStorei(GL_UNPACK_ROW_LENGTH, (int)([bitmap bytesPerRow]/samplesPerPixel));
glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
glGenTextures (1, &texName);
glBindTexture (GL_TEXTURE_RECTANGLE_ARB, texName);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
if(![bitmap isPlanar] && (samplesPerPixel == 3 || samplesPerPixel == 4))
{
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0,
samplesPerPixel == 4 ? GL_RGBA8 : GL_RGB8,
(int)[bitmap pixelsWide], (int)[bitmap pixelsHigh],
0, samplesPerPixel == 4 ? GL_RGBA : GL_RGB,
GL_UNSIGNED_BYTE, [bitmap bitmapData]);
}
} // 1
return texName;
}
ビューを OpenGL コンテキストに描画し、アクティビティ モニターでアプリのメモリ フットプリントを確認すると、アクティビティ モニターのビューが更新されるたびに、数値が約 4 MB 増加することがわかります。@autoreleasepool
コードに で示されているブロックを追加すること//1
で、更新サイクルごとに約 2 MB に減らすことができました。それでも、どんどん増えていきます。
自動解放されたオブジェクトを C++ から解放する正しい方法は何ですか?