View Full Version : CGGLContextCreate And OpenGL

09-21-2006, 03:07 PM
Hi all,

I'm attempting to leverage Core Graphics and its CGGLContext to render into my current opengl context.

CGGLContextCreate sets a number of states and matrices, so I just push them, and pop them when CGReleaseContext is called.

While I realize it's wasteful I'm creating the quartz context, drawing the text, and releasing the quartz context each time I draw. This allows me to ensure that states set by CG to draw text do not interfere with other draw calls.

The problem is that CGGLCreateContext seems to allocate memory for textures, and CGReleaseContext doesn't appear to clean it up. Drawing a simple FPS counter for a few minutes finally fails when memory can no longer be allocated.

Is anyone aware of how to properly integrate OpenGL & Core Graphics so that it doesn't leak memory everywhere?


09-22-2006, 03:15 PM
Hmm, this might help. Rather than creating a context you can save and restore the old state. I'm prety newb to core graphics. Here goes...

//from CGContext.h

//save the state

//do whatever transform

//restore what you had

09-22-2006, 10:31 PM
I tried exactly this, but it (unfortunately) doesn't work properly in terms of OpenGL. Since its the create context that sets up the new states, I need to save the context before I create one, which doesn't make any sense. :\

09-23-2006, 10:25 AM
Maybe there is a reference count issue going on. For instance, when I create the bit map I need to create a color space too.

CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
//make bit map
CGColorSpaceRelease( space );
//now release the context?

Do you think the context might be refering to the color space too? So, when you release the context its not cleaning up? I see many examples where the color space is not being released...

Just a guess. My code is creating a context too.

09-23-2006, 10:43 AM
he he, turn off loop unrolling in the compiler seems to save some memory too.

12-14-2006, 06:12 PM
Hey, dont share the opengl context with the other opengl stuff you do. And don't create and destroy everything every draw call. There is no way that will be faster than just rendering into a software buffer and calling texImage2D to upload it.

Instead keep a separate context around for your CG stuff, and keep it attached to the drawing surface as your main context.

Call CGFlushContext when you are done drawing.

It is not safe to share CG Contexts OpenGL Context with other OpenGL code, you can have texture name space collisions, etc.

I used to be on the Quartz team, so take my word for it.