   Problem with SDL_BlitSurface()
  I've been trying to create some classes to hold a bunch of SDL_Surface pointers for use as, say, a collection of tiles. Just about everything seems to work fine, except for one thing: when I call a particular function of my collection class, the Add_TileKey() function, the program will just up and exit when you start it up.Here are the relevent bits of code:#include "SDL\SDL.h"#include <string>#include <iostream>#include <vector>using namespace std;const int SCREEN_WIDTH = 640;const int SCREEN_HEIGHT = 480;const int SCREEN_BPP = 32;SDL_Surface *message = NULL;SDL_Surface *background = NULL;SDL_Surface *screen = NULL;class TileKey{ private: string name; SDL_Surface* image; public: TileKey(); TileKey(string name, SDL_Surface* image); ~TileKey();  void Set_Name(string name); void Set_Image(SDL_Surface* image);  string Get_Name(); SDL_Surface* Get_Image();   };TileKey::TileKey(){   image = NULL;}TileKey::TileKey(string name, SDL_Surface* image){   this->name = name;   this->image = image;}TileKey::~TileKey(){   if (image != NULL)   { SDL_FreeSurface(image); image = NULL;   }}class TileCollection{ private: vector<TileKey> tiles; public: TileCollection();  void Add_TileKey(TileKey tk); void Add_Image(string name, SDL_Surface* image);  SDL_Surface* Get_Image(string name); SDL_Surface* Get_Image(int index);   };TileCollection::TileCollection(){}void TileCollection::Add_TileKey(TileKey tk){   tiles.push_back(tk);}int main(int argc, char* args[]){   // init all sdl subsystems   if ( SDL_Init(SDL_INIT_EVERYTHING) == -1 )   { return 1;   } // set up the screen   screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE); // if there was an error in setting up the screen   if (screen == NULL)   { return 1;   } // set the window caption   SDL_WM_SetCaption("Hello World", NULL);    background = load_image("background.bmp");   apply_surface(0, 0, background, screen);   TileKey tk;   tk.Set_Name("hw3");   tk.Set_Image(load_image("hello_world3.bmp"));   TileCollection tc; // this line right here screws up the program;   // comment it out, and the program works perfectly   tc.Add_TileKey(tk);   SDL_Rect offset;   offset.x = 100;   offset.y = 100;   SDL_BlitSurface(tk.Get_Image(), NULL, screen, &offset);  // update the screen   if (SDL_Flip(screen) == -1)   { return 1;   } // wait 5 seconds   SDL_Delay(5000); // free the surfaces   SDL_FreeSurface(message);   SDL_FreeSurface(background); // quit SDL   SDL_Quit(); // return   return 0;}It's probably some stupid mistake I'm overlooking, but this one has been plaguing me for a while. Any suggestions would be most appreciated.EDIT: I say I'm having a problem with SDL_BlitSurface because when I run the debugger, SDL_BlitSurface is where it ends up failing.   
  If you have code like -{SDL_Surface* surf = SDL_LoadBMP( "myImage.bmp" );TileKey myTileKey( surf, "blah" );myTileCollection.add_key( myTileKey );}// myTileKey is destroyed at this point, which// has called SDL_FreeSurface() on surf.What will happen on the last line is that a copy of myTileKey will be created and put in your collection. myTileKey, however, after leaving the scope in which is was declared will be destroyed, calling the destructor and freeing its SDL_Surface. Since both it and the copy have a pointer to the same surface, the one in the collection now points to an invalid surface. Passing an invalid surface pointer to the blit call is causing it to crash.   ;  Ah, that was it! Thank you so much for your help, you've spared me a great deal of aggravation. =D   ;  Hi, you might already know may that you can fix this by making copy constructors and overload the asignment operator. Just want to mention two possibilities:1. (nice) implement the *image pointer in tilekey as a boost::shared_ptr and copy those in the copy constructor and assignment operator. Or use vector<boost::shared_ptr<TileKey> > tiles as the datastructure for TileCollection.2. (hack?) SDL_Surface struct has a member called refcount. If it's larger than 1, SDL_FreeSurface does not delete the surface but decrements it by one. So you could use this to implement reference counting in the assignment operator and copy constructor of TileKey by incrementing this member by one.EDIT: option 2 is not so hack, the sdl doc wiki says:Quote:The proper way to duplicate a surface is  SDL_Surface *Srfc1, *Srfc2;  Srfc1= IMG_Load("foo.png");  Srfc2= Srfc1;  Srfc2->refcount++;  ..  SDL_FreeSurface(Srfc1);  SDL_FreeSurface(Srfc2);