While I'm trying to close program, I want to delete dynamic SDL_surface array as usual through the for-loop, but program finishes with exit code -1073740940. When I commenting this loop, program closes with 0 exit code. So, what's the problem? Here is code:
void close(SDL_Window* window, SDL_Surface* surface, SDL_Surface* keys[]) {
//free memory
for( int i = 0; i < KEY_PRESS_SURFACE_TOTAL; ++i )
{
//KEY_PRESS_SURFACE_TOTAL is defined in enum
SDL_FreeSurface(keys[i]);
keys[i] = nullptr;
}
SDL_FreeSurface(surface);
surface = nullptr;
SDL_DestroyWindow(window);
window = nullptr;
SDL_Quit();
I'm a beginner, so I'm just trying to learn SDL2 basics.
UPDATE: Here is enum section code:
enum KeyPressSurfaces {
KEY_PRESS_SURFACE_DEFAULT,
KEY_PRESS_SURFACE_UP,
KEY_PRESS_SURFACE_DOWN,
KEY_PRESS_SURFACE_LEFT,
KEY_PRESS_SURFACE_RIGHT,
KEY_PRESS_SURFACE_TOTAL };
Also here's the using this array in code:
bool loadMedia(SDL_Surface* keys[]) {
bool success = true;
keys[KEY_PRESS_SURFACE_DEFAULT] = loadSurface("images/press.bmp");
if( keys[KEY_PRESS_SURFACE_DEFAULT] == NULL ) {
std::cout << "Failed to load default image!\n";
success = false;
} //and so on...
return success; }
In main section...
SDL_Surface* gKeyPressSurfaces[KEY_PRESS_SURFACE_TOTAL] = { };
//creating window, surface etc.
if ( !loadMedia(gKeyPressSurfaces) )
std::cout << "Failed to load media!\n";
else {
bool quit = false;
SDL_Event event;
gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_DEFAULT];
while ( !quit )
{
while (SDL_PollEvent(&event) != 0)
{
if ( event.type == SDL_KEYDOWN ) {
switch (event.key.keysym.sym) {
case SDLK_UP:
gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_UP];
break; // and so on...
I did it following LazyFoo's SDL2 tutorials, so I don't understand why this not working.
SDL_FreeSurface(keys[i]); // this line sets keys[i] to nullptr keys[i] = nullptr; // you are accessing a nullptr already in this line
comment out keys[i] = nullptr; line then your code will work.
User contributions licensed under CC BY-SA 3.0