   c++ SDL_FreeSurface crashing
  So i plan on storing SDL_Surfaces in a vector rather than just their pointers, so I created a vectorstd::vector<SDL_Surface> surfaceList;and then add several surfaces to it in the fashion:    //CSurface.cpp    SDL_Surface* CSurface::onLoad(char* file) {    SDL_Surface *surfTemp = NULL;    SDL_Surface *surfReturn = NULL;    if((surfTemp = SDL_LoadBMP(file)) == NULL) {  return NULL;    }    surfReturn = SDL_DisplayFormat(surfTemp);    SDL_FreeSurface(surfTemp);    return surfReturn;    }    //CApp_LoadGraphics    surfHolder = NULL;if((surfHolder = SDL_SetVideoMode(width,height,32,SDL_HWSURFACE | SDL_DOUBLEBUF)) == NULL) {return false;}surfaceList.push_back((*surfHolder));//load graphical imagesif((surfHolder = CSurface::onLoad("test.bmp")) == NULL) {return false;}CSurface::transparent(surfHolder,255,255,255);surfaceList.push_back((*surfHolder));//Animation Section if((surfHolder = CSurface::onLoad("testAnim.bmp")) == NULL) {return false;}CSurface::transparent(surfHolder,255,255,255);surfaceList.push_back((*surfHolder));    surfHolder = NULL;but on attempting to free the surfaces by calling//CApp_OnCleanup.cppfor(unsigned int i = 0; i < surfaceList.size(); i++) {SDL_FreeSurface(&(surfaceList.at(i)));}my program breaks due to the a corruption of a heap on the first iteration upon closing even though i've verified that &(surfaceList.at(0)) points to 014E5DD8, which should house the main screen??   
  Did you read the SDL_Surface documentation?  There are several "red flags" in your code:1) Deleting a statically allocated object:int* t = new int;   // SDL_Surface* s = SDL_LoadImage( ... );int y = *t;    // vec.push_back( *s );delete &y; // &y != t; crash! // SDL_FreeSurface( &vec[0] );2) Freeing the surface returned by SDL_SetVideoMode.  This is not needed as (according to the documentation) the video surface is freed automatically on SDL_Quit.Here are some things you can do:1) Use an array of pointer-to SDL_Surfaces, std::vector<SDL_Surface*>int* t = new int;    // SDL_Surface* s = SDL_LoadImage( ... );int* y = t;// vec.push_back( s );delete y; // y == t; ok!  // SDL_FreeSurface( vec[0] );2) Or, use smart pointers to SDL_Surfaces:shared_ptr<SDL_Surface> ptr( load_image( "./test.bmp" ), SDL_FreeSurface );vector<shared_ptr<SDL_Surface> > vec;vec.push_back( ptr );// etc;  Thank you for pointing out the documentation, i discovered the problem was not incrementing the refcount.However, my only question refers in your explanation of deleting a statically allocated object when you sayint y = *t; // vec.push_back( *s );doesn't  the vector dynamically allocate the memory for the surface? So when i go to SDL_FreeSurface( &vec[0] ); isn't that deleting the dynamic memory?although i will  change the vector as you suggested to a vector of pointers, which I now realizemakes more sense.   ;  SDL_FreeSurface() expects to be able to free the allocation for the surface itself, as well as any subobjects for the pixel format and data. You cannot pass it a pointer to inside a vector's allocation and expect it to work.The problem is that SDL_Surfaces aren't designed to be manipulated by value. Moving to a vector of (smart) pointers will work.   