NSOpenGLContext Memory Leak
NSOpenGLContext Memory Leak
- Subject: NSOpenGLContext Memory Leak
- From: Richard Charles <email@hidden>
- Date: Sat, 28 Oct 2006 00:13:15 -0600
I am developing an OpenGL Cocoa DOCUMENT based application. When
using NSOpenGLView there is a memory leak of 32k every time the
document window is closed. The leak is from the OpenGL context.
If I create a custom OpenGLView class following the newly released
"OpenGL Programming Guide for Mac OS X" and the sample code "Custom
Cocoa OpenGL" the same thing happens. I get a 32k OpenGL context
memory leak each time the document window is closed.
To narrow the problem down I created a very simple application which
creates an OpenGL context when the document window is initialized.
When the window is closed and the dealloc method is called the
context is released. Here is the code.
- (id)initWithFrame:(NSRect)frameRect
{
NSOpenGLPixelFormatAttribute attrs[] = { 0 };
format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
context = [[NSOpenGLContext alloc] initWithFormat:format
shareContext:nil];
self = [super initWithFrame:frameRect];
return self;
}
- (void)dealloc
{
[context release];
[format release];
[super dealloc];
}
This code will will have a 32k context leak for each document window
closed. Cocoa is still holding onto an OpenGL context object of some
sort even though the context object accepts the release message
without error.
If you add a clear current context message to the dealloc method as
follows then the leak disappears.
- (void)dealloc
{
[NSOpenGLContext clearCurrentContext];
[context release];
[format release];
[super dealloc];
}
It is interesting to note in the above code that the context will
still accept a release message even though it was apparently fully
disposed of with the prior clearCurrentContext message (as confirmed
by testing).
Here are my questions that I can not figure out!
1. What is the proper way to dispose of an OpenGLContext object in
Cocoa for DOCUMENT based applicatons?
2. If [NSOpenGLContext clearCurrentContext] completely disposes of
the context object, why does the context later accept a [context
release] message?
3. Why is +(void)clearCurrentContext a class method of
NSOpenGLContext? I understand how class methods can create objects
but here is a class method which disposes of an object!
4. How does [NSOpenGLContext clearCurrentContext] know which instance
to act on? In the code sample above I never said [context
makeCurrentContext], so how does it know?
Sorry if I am making a mountain out of a mole hill here but this has
been driving me crazy! If anyone could enlighten me, it would be
appreciated.
Regards,
Richard Charles
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden