user1534664
user1534664

Reputation: 3418

C++ SDL Initialize everything & getting runtime error

I just tried to omit the SDL_Init() function, but everything is still working. By everything I mean the audio and graphics. Does this mean this function is useless?

Also, I'm getting a runtime error when I close the program: "This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information"

What can be causing this? I'm closing everything properly:

SDL_FreeSurface(Screen);
SDL_FreeSurface(Message);
Mix_FreeMusic(mus2);
TTF_CloseFont(Font);
TTF_Quit();
SDL_Quit();

These are the only surfaces/fonts/music im using:

//The surfaces
TTF_Font *Font = NULL;
SDL_Surface *Screen = NULL;
SDL_Surface *Message = NULL;
Mix_Music *mus2 = Mix_LoadMUS("./music.mp3");

If the entire source code is required then ask!

Edit: The error was due to me trying to free the Screen surface!

Upvotes: 0

Views: 689

Answers (2)

user1534664
user1534664

Reputation: 3418

The error was due to me trying to free the Screen surface, which is actually a task that SDL_Quit() does. I dont know why I was doing it :P

Upvotes: 1

Jordan Ell
Jordan Ell

Reputation: 1805

The main functionality of SDL_Init() seems to be error handling (other than initializing various subsystems). From the source, SDL_Init() calls the function below:

void SDL_InstallParachute(void)
{
/* Set a handler for any fatal signal not already handled */
int i;
#ifdef HAVE_SIGACTION
struct sigaction action;

for ( i=0; SDL_fatal_signals[i]; ++i ) {
    sigaction(SDL_fatal_signals[i], NULL, &action);
    if ( action.sa_handler == SIG_DFL ) {
        action.sa_handler = SDL_Parachute;
        sigaction(SDL_fatal_signals[i], &action, NULL);
    }
}
#ifdef SIGALRM
/* Set SIGALRM to be ignored -- necessary on Solaris */
sigaction(SIGALRM, NULL, &action);
if ( action.sa_handler == SIG_DFL ) {
    action.sa_handler = SIG_IGN;
    sigaction(SIGALRM, &action, NULL);
}
#endif
#else
void (*ohandler)(int);

for ( i=0; SDL_fatal_signals[i]; ++i ) {
    ohandler = signal(SDL_fatal_signals[i], SDL_Parachute);
    if ( ohandler != SIG_DFL ) {
        signal(SDL_fatal_signals[i], ohandler);
    }
}
#endif /* HAVE_SIGACTION */
return;
}

This function sets up a safety net which can handle runtime errors that the initialized subsystems do not seem to handle. This probably explains the runtime error you get while exiting your application because that would probably not be handled by a subsystem.

In short, you should always call the initializing methods of libraries if the developers would have expected it to be called just to be safe.

Upvotes: 0

Related Questions