title: exc_bad_access error xcode5 c++ sdl2
body: <p>I've been toying with this code a guy on youtube attached to one of his tutorial videos... I've run into a few problems that I cant seem to get past... The current one is this 'EXC_BAD_ACCESS' error... From what I've researched briefly online these errors occur when you improperly use the stack and allocate memory to things that no longer exist or something like that... I just can't pinpoint what I'm doing wrong exactly... I've seen some troubleshooting done -- but nothing for xcode 5 yet</p>
  
  <p>The error shows up in a class called 'cSprite.cpp':</p>
  
  <pre><code>// ~~~~~~~~~~~~~~~~~~~     cSprite.cpp      ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  
  #include "stdafx.h"
  #include "cSprite.h"
  //#include "gMain.h"
  //#include &lt;SDL2/SDL_main.h&gt;
  //#include &lt;SDL2/SDL.h&gt;
  //#include "cSDL_Setup.h"
  
  using namespace std;
  
  void draw(SDL_Renderer, SDL_Texture, SDL_Rect);
  
  cSprite::cSprite(SDL_Renderer* passed_renderer, string filePath, int x, int y, int w, int h)
  {
      renderer = passed_renderer;
  
      //... image
      image = NULL;
      image = IMG_LoadTexture(renderer,filePath.c_str());
  
      // image error message
      if (image == NULL)
      {
          cout&lt;&lt;"Couldnt load image..."&lt;&lt;filePath.c_str()&lt;&lt;endl;
      }
  
      //image dimensions
      rect-&gt;x = x;            // ***** Error: Thread 1:EXC_BAD_ACCESS(code=EXC_1386_GPFLT)
      rect-&gt;y = y;
      rect-&gt;w = w;
      rect-&gt;h = h;
  
  };
  
  cSprite::~cSprite(void)
  {
      SDL_DestroyTexture(image);
  }
  
  //get methods
  //SDL_Rect* cSprite::getRect()
  //{
  //    return rect;
  //}
  
  //get methods
  //SDL_Texture* cSprite::getImage()
  //{
  //    return image;
  //}
  
  void cSprite::draw()
  {
      SDL_RenderCopy(renderer, image, NULL, rect);
  }
  </code></pre>
  
  <p>... the line 'rect->x = x;' </p>
  
  <p>gets the error I put beside it...but idk what else could be causing it... I was hoping someone could explain why EXC_BAD_ACCESS errors even occur... and/or where in the rest of my code it could be coming from...</p>
  
  <p>here's my 3 other .cpp files</p>
  
  <p>....</p>
  
  <pre><code>// ~~~~~~~~~~~~~~~~~~~~~~~~        gMain.cpp        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  #include &lt;iostream&gt;
  #include "stdafx.h"
  #include "gMain.h"
  
  
  using namespace std;
  
  gMain::gMain(int passed_screenWidth, int passed_screenHeight)
  {
      screenWidth = passed_screenWidth;
      screenHeight = passed_screenHeight;
  
      // quit boolean
      quit = false;
  
      // create instance of cSDL class
      csdl_setup = new cSDL_Setup(&amp;quit, screenWidth, screenHeight);
  
      grass = new cSprite(csdl_setup-&gt;getRenderer(), "/Users/jamesbryant/Desktop/nuGame/nuGame/images.jpeg", screenWidth, screenHeight, screenWidth, screenHeight);
  
      bruce_Lee = new cSprite(csdl_setup-&gt;getRenderer(), "/Users/jamesbryant/Desktop/nuGame/nuGame/lee.bmp", 300, 300, 200, 200);
  
  
  }
  
  gMain::~gMain(void)
  {
  }
  
  void gMain::gameLoop(void)
  {
      // game loop logic
      while (!&amp;quit &amp;&amp; csdl_setup-&gt;getMainEvent() -&gt;type != SDL_QUIT)
      {
          csdl_setup-&gt;begin();
  
          grass-&gt;draw();
          bruce_Lee-&gt;draw();
  
  
          csdl_setup-&gt;end();
  
      }
  }
  </code></pre>
  
  <p>....</p>
  
  <pre><code>// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ cSDL_Setup.cpp ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  #include "stdafx.h"
  #include "cSDL_Setup.h"
  //#include "gMain.h"
  
  using namespace std;
  
  cSDL_Setup::cSDL_Setup(bool* quit, int screenWidth, int screenHeight)
  {
      // create window
      window = NULL;
      window = SDL_CreateWindow("rWDY_pWDR", 400, 400, screenWidth, screenHeight, SDL_WINDOW_RESIZABLE);
  
      // if window couldnt be created...
      if (window == NULL)
      {
          cout&lt;&lt;"Window couldnt be created..."&lt;&lt;endl;
  
          *quit = true;
          //exit(0);
      }
  
      //create renderer
      renderer = NULL;
      renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
  
      // initialize images
      mainEvent = new SDL_Event();
  
  }
  
  cSDL_Setup::~cSDL_Setup(void)
  {
      SDL_DestroyWindow(window);
      SDL_DestroyRenderer(renderer);
      delete mainEvent;
  }
  
  //get methods
  SDL_Renderer* cSDL_Setup::getRenderer()
  {
      return renderer;
  }
  
  SDL_Event* cSDL_Setup::getMainEvent()
  {
      return mainEvent;
  }
  
  void cSDL_Setup::begin()
  {
      SDL_PollEvent(mainEvent);
      SDL_RenderClear(renderer);
  }
  
  void cSDL_Setup::end()
  {
      SDL_RenderPresent(renderer);
  
  }
  </code></pre>
  
  <p>....</p>
  
  <pre><code>// ~~~~~~~~~~~~~~~~~~~~~~~~~~ firstGame.cpp ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  #include "stdafx.h"
  #include "cSDL_Setup.h"
  //#include "gMain.h"
  
  using namespace std;
  
  cSDL_Setup::cSDL_Setup(bool* quit, int screenWidth, int screenHeight)
  {
      // create window
      window = NULL;
      window = SDL_CreateWindow("rWDY_pWDR", 400, 400, screenWidth, screenHeight, SDL_WINDOW_RESIZABLE);
  
      // if window couldnt be created...
      if (window == NULL)
      {
          cout&lt;&lt;"Window couldnt be created..."&lt;&lt;endl;
  
          *quit = true;
          //exit(0);
      }
  
      //create renderer
      renderer = NULL;
      renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
  
      // initialize images
      mainEvent = new SDL_Event();
  
  }
  
  cSDL_Setup::~cSDL_Setup(void)
  {
      SDL_DestroyWindow(window);
      SDL_DestroyRenderer(renderer);
      delete mainEvent;
  }
  
  //get methods
  SDL_Renderer* cSDL_Setup::getRenderer()
  {
      return renderer;
  }
  
  SDL_Event* cSDL_Setup::getMainEvent()
  {
      return mainEvent;
  }
  
  void cSDL_Setup::begin()
  {
      SDL_PollEvent(mainEvent);
      SDL_RenderClear(renderer);
  }
  
  void cSDL_Setup::end()
  {
      SDL_RenderPresent(renderer);
  
  }
  </code></pre>
  
  <p>... here's what my cSprite.h file looks like right now:</p>
  
  <pre><code>#ifndef __game__cSprite__
  #define __game__cSprite__
  #pragma once
  
  #include &lt;iostream&gt;
  #include &lt;SDL2/SDL_main.h&gt;
  #include &lt;SDL2/SDL.h&gt;
  
  using namespace std;
  
  
  class cSprite
  {
  public:
      cSprite(SDL_Renderer* passed_renderer, string filePath, int x, int y, int w, int h);
      ~cSprite(void);
  
      void draw();
  
  private:
      SDL_Texture* image = NULL;
  
      SDL_Rect* rect = NULL;
  
      SDL_Renderer* renderer = NULL;
  
  };
  
  #endif /* defined(__game__cSprite__) */
  </code></pre>