title: gldrawelements bad access in xcode when used outside of GLKViewController
body: <p>I'm pretty new to OpenGL ES, but all I'm trying to do is draw indexed vertices using <code>glDrawElements</code> in a <code>Character</code> class. I've gotten this to work before inside of my <code>GLKViewController</code> class, but when I tried creating a <code>Character</code> class which would perform its own rendering, I got nothing but BAD_ACCESS. Here is my <code>Character</code> class:</p>
  
  <pre><code>#import "Character.h"
  
  @interface Character()
  {
      GLuint _vertexBuffer;
      GLuint _indexBuffer;
      GLuint _vertexArray;
  }
  
  @property(nonatomic, weak) GLKBaseEffect *effect;
  
  @end
  
  typedef struct {
      float Position[3];
      float Color[4];
  } Vertex;
  
  const Vertex Vertices[] = {
      {{1, -1, 0}, {1, 0, 0, 1}},
      {{1, 1, 0}, {0, 1, 0, 1}},
      {{-1, 1, 0}, {0, 0, 1, 1}},
      {{-1, -1, 0}, {0, 0, 0, 1}}
  };
  
  const GLushort Indices[] = {
      0, 1, 2,
      2, 3, 0
  };
  
  @implementation Character
  
  - (id)initWithEffect:(GLKBaseEffect *)effect
  {
      if (self = [super init])
      {
          self.effect = effect;
          [self setupGL];
      }
  
      return self;
  }
  
  - (void)setupGL
  {
      glGenVertexArraysOES(1, &amp;_vertexArray);
      glBindVertexArrayOES(_vertexArray);
  
      glGenBuffers(1, &amp;_vertexBuffer);
      glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
      glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
  
      glGenBuffers(1, &amp;_indexBuffer);
      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
      glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);
  
  
      glEnableVertexAttribArray(GLKVertexAttribPosition);
      glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Position));
      glEnableVertexAttribArray(GLKVertexAttribColor);
      glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Color));
  
  
      glBindVertexArrayOES(0);
  }
  
  - (void)teardownGL
  {
      glDeleteBuffers(1, &amp;_vertexBuffer);
      glDeleteBuffers(1, &amp;_indexBuffer);
  }
  
  - (void)render
  {
      self.effect.transform.modelviewMatrix = GLKMatrix4Translate(GLKMatrix4Identity, self.position.x, self.position.y, self.position.z);
      self.effect.transform.modelviewMatrix = GLKMatrix4Rotate(self.effect.transform.modelviewMatrix, self.rotation, 0.0f, 0.0f, 1.0f);
  
      [self.effect prepareToDraw];
      glBindVertexArrayOES(_vertexArray);
      glDrawElements(GL_TRIANGLES, sizeof(Indices) / sizeof(Indices[0]), GL_UNSIGNED_SHORT, 0);
  }
  
  @end
  </code></pre>
  
  <p>Then in <code>ViewController</code></p>
  
  <pre><code>- (void)viewDidLoad
  {
      [super viewDidLoad];
  
      self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
  
      GLKView *view = (GLKView *)self.view;
      view.context = self.context;
  
      character = [[Character alloc] initWithEffect:self.effect];
      character.position = GLKVector3Make(self.view.bounds.size.width / 2, self.view.bounds.size.height / 2, 0.0f);
      [self setupGL];
  }
  </code></pre>
  
  <p>rendered using:</p>
  
  <pre><code>- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
  {
      glClearColor(0.65f, 0.65f, 0.65f, 1.0f);
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  
      [character render];
  }
  </code></pre>
  
  <p>I know it isn't something as simple as a miscalculation of byte-size or something because I've been at this for a couple days now. </p>