Simo Virokannas

Writings and ramblings

Xcode, NSOpenGLView, internal crash

I’ve been encountering a frequent problem with Xcode interface builder with NSOpenGLViews – whenever you add one, connect it to a class, and touch something, interface builder starts crashing like mad. First, you try to save everything hitting continue, then you realize saving, restarting, loading, deleting the control, creating it again, nothing helps. You just -can’t- add OpenGL views anymore.

This happens once per project, and for me it seemed there’s no other solution than to create a completely new XIB.

Today, I found a workaround:

  1. Remove the NSOpenGLView that’s causing the crash
  2. Restart Xcode, reload the project
  3. (never touch NSOpenGLView in the interface builder again)
  4. Add a “Custom NSView” and move it into place, set all the NSView settings for it as you would with the OpenGLView
  5. Set the class for that custom view to NSOpenGLView or a class derived from that
  6. For the view controller, add an IBOutlet NSOpenGLView *object or the derived class
  7. Connect that outlet to the Custom NSView in interface builder
  8. Add some proper initialization code for the view (straight from Cocoa API documentation) – this is what you don’t have to do when using the NSOpenGLView but now it’s necessary:
// in the controller class .m
- (void)awakeFromNib {
 // set the pixel format and depth buffer and other parameters as you wish
 NSOpenGLPixelFormatAttribute attrs[] = {
 NSOpenGLPFADoubleBuffer,
 NSOpenGLPFADepthSize, 32,
 0
 };
 NSOpenGLPixelFormat *pf = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];

 // assuming your class has an IBOutlet NSOpenGLView *m_gl
 [m_gl initWithFrame:[m_gl frame] pixelFormat:pf];
}

… and you’re set. No crashing.

Edit: this only seems to happen on an Xcode older than 4.2 or on 4.2 that’s not a clean install but updated from 4.1 or older.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.