   Crashy SDL Screenshot Function
  I adapted the function posted in this thread to my code for taking a screenshot, but it crashes when I hit the key associated.  Here's my version (only minor changes):void System::saveScreenshotBmp(char *filename){SDL_Surface *temp;unsigned char *pixels;if (SDL_BYTEORDER == SDL_LIL_ENDIAN){temp = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x000000FF, 0x0000FF00, 0x00FF0000, 0);}else{temp = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x00FF0000, 0x0000FF00, 0x000000FF, 0);}if (temp != NULL) {pixels = new unsigned char[3 * SCREEN_WIDTH * SCREEN_HEIGHT];if (pixels != NULL) {glReadPixels(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, pixels);for (int i = 0; i < SCREEN_HEIGHT; i++){memcpy(((char *) temp->pixels) + temp->pitch * i, pixels + 3 * SCREEN_WIDTH * (SCREEN_HEIGHT-i-1), SCREEN_WIDTH * 3);}free(pixels);SDL_SaveBMP(temp, filename);}SDL_FreeSurface(temp);delete pixels;}cout << "Screenshot saved as '" << filename << "'"<< endl;}I figured this might be a good time to ask how one usually debugs fullscreen programs.  Only by running a windowed version?   
  Use delete[] pixels; instead of free(pixels); and delete pixels;  See 16.3.Also, pixels is deleted twice:if (pixels != NULL) {glReadPixels(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, pixels);for (int i = 0; i < SCREEN_HEIGHT; i++){memcpy(((char *) temp->pixels) + temp->pitch * i, pixels + 3 * SCREEN_WIDTH * (SCREEN_HEIGHT-i-1), SCREEN_WIDTH * 3);}delete[] pixels; // First deletion, ok!SDL_SaveBMP(temp, filename);}SDL_FreeSurface(temp);delete[] pixels; // Second deletion, boom!;  That did it.  Thanks!   ;  I'm using this function which I wrote a while back in another program to check for existing .bmp files and provide an available filename as a char* to the function above, but I'm getting garbled filenames with out a .bmp extensioncase SDLK_PRINT:    systemPointer->savePrtScrnBmp(systemPointer->queryPrtScrnFilename());    break;char* System::queryPrtScrnFilename(){string filename = "Screenshot_";int fcounter = 0;ostringstream formatter;formatter << filename << fcounter << ".bmp";filename = formatter.str();const char* fileConstStr = filename.c_str();ifstream fin(fileConstStr);while (!fin.fail()){ostringstream formatter;fin.close();fcounter++;filename.clear();filename = "Screenshot_";formatter << filename << fcounter << ".bmp";filename = formatter.str();fileConstStr = filename.c_str();}return filename.data();};  The value returned from std::string::data is not null-terminated. Use c_str.   ;  Okay SDL_SaveBMP( , ); seemed to only want a non-const char*, it works now that I changed the return type to a string and gave SaveBMP the string like thisSDL_SaveBMP(temp, filename.c_str());Also for anyone who wanted to use the code I accidentally took out a line that was affecting the while loop and giving incorrect checking of filenames, the corrected bit ads 1 line to the bottomwhile (!fin.fail()){ostringstream formatter;fin.close();fcounter++;filename.clear();filename = "Screenshot_";formatter << filename << fcounter << ".bmp";filename = formatter.str();fileConstStr = filename.c_str();fin.open(fileConstStr);}