NSOpenGLContext Memory Leak

  • 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
previous month october 2006 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today