title: OpenGL ES - purple or black screen
body: <p>I'm using OpenGL ES in my iPhone application and sometimes during startup the screen goes purple or black - in one case from twenty. This happens only during initialization and if the screen became black or purple it will remain in such a color - only restarting the application helps.
  Also I found out that when this bug happens, application is running (I can see the game loop working through console), but the iPhone stops responding to touches (touchesBegan method is not being invoked).</p>
  
  <p>Here's my code:</p>
  
  <pre><code>#define USE_DEPTH_BUFFER FALSE
  
  @implementation EAGLView
  
  @synthesize context;
  
  + (Class)layerClass {
      return [CAEAGLLayer class];
  }
  
  - (id)initWithFrame: (CGRect)frame {
      NSLog(@"init EAGLView");
      if ( (self = [super initWithFrame: frame]) ) {
          NSLog(@"initializing CAEAGLLayer and EAGLContext");
          // Get the layer
          CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
  
          eaglLayer.opaque = YES;
          eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
                                          [NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
  
          context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
  
          if (!context || ![EAGLContext setCurrentContext:context]) {
              NSLog(@"!context || ![EAGLContext setCurrentContext:context]");
              [self release];
              return nil;
          }
      }
      return self;
  }
  
  - (void)layoutSubviews {
      [EAGLContext setCurrentContext: context];
      [self destroyFramebuffer];
      [self createFramebuffer];
  }
  
  
  - (BOOL)createFramebuffer {
  
      glGenFramebuffersOES(1, &amp;viewFramebuffer);
      glGenRenderbuffersOES(1, &amp;viewRenderbuffer);
  
      glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
      glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
      [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.layer];
      glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);
  
      glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &amp;backingWidth);
      glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &amp;backingHeight);
  
      if (USE_DEPTH_BUFFER) {
          glGenRenderbuffersOES(1, &amp;depthRenderbuffer);
          glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
          glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight);
          glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
      }
  
      if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) {
          NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
          return NO;
      }
      return YES;
  }
  
  
  - (void)destroyFramebuffer {
  
      glDeleteFramebuffersOES(1, &amp;viewFramebuffer);
      viewFramebuffer = 0;
      glDeleteRenderbuffersOES(1, &amp;viewRenderbuffer);
      viewRenderbuffer = 0;
  
      if(depthRenderbuffer) {
          glDeleteRenderbuffersOES(1, &amp;depthRenderbuffer);
          depthRenderbuffer = 0;
      }
  }
  
  -(void) startDrawing: (GLfloat) viewWidth andHeight: (GLfloat) viewHeight
  {
      [EAGLContext setCurrentContext: context]; 
      glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer); 
      glViewport(0, 0, backingWidth, backingHeight); 
      glMatrixMode(GL_PROJECTION); 
      glLoadIdentity();
      glRotatef(-90.0f, 0.0f , 0.0f, 1.0f);
  
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
  
      glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 
      glClear(GL_COLOR_BUFFER_BIT); 
  } 
  
  -(void) endDrawing
  { 
      glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer); 
      [context presentRenderbuffer: GL_RENDERBUFFER_OES]; 
  }
  
  - (void)dealloc {
      if ([EAGLContext currentContext] == context) {
          [EAGLContext setCurrentContext:nil];
      }
  
      [context release];  
      [super dealloc];
  }
  
  ...touches processing...
  
  @end
  </code></pre>
  
  <p>What may be the problem and how can I fix it?</p>
  
  <p>Thank you in advance,
  Ilya.</p>