SDL doesn't exit cleanly!

Programming, for all ages and all languages.
Post Reply
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

SDL doesn't exit cleanly!

Post by earlz »

I have got SDL working and everything now, but it doesn't exit cleanly, it always crashes..

Basically, my layout is that I have my main() thread doing the actual code needed..but for now, it just clears the screen and goes to an infinite loop waiting for the quit_all variable to change to 1 Then I have a SDL thread made for checking and handling the messages of the video display.

here is the main points of my code...(condensed)

Code: Select all

int Thread_Messaging(void * arg){
	if( SDL_Init(SDL_INIT_VIDEO) == -1 ){exit(1);}
	display.screen=SDL_SetVideoMode(640,480,8,SDL_HWSURFACE);
	display.vbuffer=new char[640*480*1];

    volatile unsigned int quit=0;
	while(quit==0){
		display.FlushBuffer();
		display.Render();
		if(SDL_PollEvent(&event)==1){
			switch(event.type){
				case SDL_QUIT:
				quit=1;
				quit_all=1;
				exit(0);
				break;
			}
		}else{
			//SDL_Delay(50); //don't waste all the processing time..
		}
	}
	quit_all=1;
	//SDL_QuitSubSystem(SDL_INIT_VIDEO);
	//while(quit_all==1){}
	SDL_Quit();
	//exit(0);
	//while(1){}

}

int main(int argc,char **argv)
#endif
{
    // initialize SDL video

	fprintf(stderr,"y");
	if ( SDL_Init( SDL_INIT_EVENTTHREAD ) < 0 )
    {
       cout<< "Unable to init SDL: " << SDL_GetError() << endl;
        exit(1);
    }

   // if( SDL_Init(SDL_INIT_VIDEO) == -1 ){exit(1);}
    // make sure SDL cleans up before exit
    //atexit(SDL_Quit);
    // create a new window
    messaging=SDL_CreateThread(Thread_Messaging,0);
    //disk_cache=SDL_CreateThread(Thread_DiskCache,0);
    fprintf(stderr,"t");
    cout << "Waiting for SDL...";
    SDL_Delay(1000);
    cout << endl <<"done!";

    DoRobots();

    // all is well ;)
    while(quit_all==0){
    	if(quit_all==1){
			quit_all++;
			SDL_WaitThread(messaging, 0);
			SDL_Quit();
		}
	} //infinite loop...
	//while(1){}
    return 0;
}

btw, nothing with my destructors do stuff with SDL, so that part is safe..
User avatar
Kevin McGuire
Member
Member
Posts: 843
Joined: Tue Nov 09, 2004 12:00 am
Location: United States
Contact:

Post by Kevin McGuire »

I am not sure. You would have to include some sort of error information and even then I might not be able to help.

I use SDL port for Linux though and my knowledge of the API is trivial.
nick8325
Member
Member
Posts: 200
Joined: Wed Oct 18, 2006 5:49 am

Post by nick8325 »

You seem to call SDL_Quit twice, one from main() and once from Thread_Messaging(). Is that right?
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post by earlz »

I've tried it a lot of ways..so I've tried it in both and only one
nick8325
Member
Member
Posts: 200
Joined: Wed Oct 18, 2006 5:49 am

Post by nick8325 »

That's odd.

I could run your code and see if I can find the problem, if you attach it here.
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post by earlz »

I'm not sure if it'll work or not in *nix, but you can use

Code: Select all

svn://brynet.biz.tm/robots_ai/
or http://svn.brynet.biz.tm/listing.php?repname=Robot-AI&path=%2F&sc=0
edit:
edited to be kinder..
just remove the spaces between the :// and url
Last edited by earlz on Mon Jun 04, 2007 10:03 am, edited 2 times in total.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

As well as what nick says, you also shouldn't issue an SDL_Init twice, use SDL_InitSubSystem and SDL_QuitSubSystem instead and only use the init/quit pair once.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
nick8325
Member
Member
Posts: 200
Joined: Wed Oct 18, 2006 5:49 am

Post by nick8325 »

It works here...

I wonder if the problem is all the stuff you did to fix those link errors. It looks like SDL is meant to supply its own WinMain function - perhaps that initialises some things itself. You could try making a GUI (not console) program, by linking with -lmingw32 -lSDLmain -lSDL. (That's what http://www.spacejack.org/games/mingw/mingw-sdl.html says to use.)

If you want to use the console too, you can make one by calling AllocConsole.
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post by earlz »

grrr..NO!
if that messes it up, I'd rather just have it crash on exiting..
if you include mingw32 then the console doesn't work, and a console is already allocated, mingw32 is just a btch and decides to invalidate the stdout handles for no real reason..
then I want this to be a portable project, so those console functions won't work..

and it doesn't have it's own WinMain, if you don't include mingw32, then it says undefined reference to it when using SDL....
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

From some mailinglist:
under win32 SDL routes stdin,stdout & stderr to stdin.txt,
stdout.txt & stderr.txt respectively if there is no console window
available. I think you would need to edit and re-compile the library if you
wanted something else.
using AllocConsole and friends won't work. I tried that often enough to know :(
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
nick8325
Member
Member
Posts: 200
Joined: Wed Oct 18, 2006 5:49 am

Post by nick8325 »

Does http://osdir.com/ml/lib.sdl/2003-02/msg00516.html help at all?

I've got a windows installation in VMware somewhere, so I'll install mingw in that in a minute and see if I can find anything.
nick8325
Member
Member
Posts: 200
Joined: Wed Oct 18, 2006 5:49 am

Post by nick8325 »

I just tried in mingw, and the instructions on that web page do make the console work. Here's what I did:
  • * Call the main function main(), instead of calling it WinMain on win32.
    * At the end of the file, add the following code:

    Code: Select all

    #ifdef WIN32
    #undef main
    extern "C" int console_main(int argc, char ** argv);
    int main(int argc, char ** argv)
    {
        return console_main(argc, argv);
    }
    * Change LDFLAGS in the Makefile to -lSDLmain -lSDL.
I don't know whether this will fix the crashing problem, because the original code didn't crash when I ran it.
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post by earlz »

It's a race condition..
For whatever reason, the first time I run it after compiling, it usually works(but not always) but if I run it again, it almost always crashes
Post Reply