Page 1 of 1

SDL doesn't exit cleanly!

Posted: Sun Jun 03, 2007 4:25 pm
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..

Posted: Sun Jun 03, 2007 6:27 pm
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.

Posted: Sun Jun 03, 2007 6:41 pm
by nick8325
You seem to call SDL_Quit twice, one from main() and once from Thread_Messaging(). Is that right?

Posted: Sun Jun 03, 2007 6:45 pm
by earlz
I've tried it a lot of ways..so I've tried it in both and only one

Posted: Sun Jun 03, 2007 6:57 pm
by nick8325
That's odd.

I could run your code and see if I can find the problem, if you attach it here.

Posted: Sun Jun 03, 2007 8:49 pm
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

Posted: Mon Jun 04, 2007 3:03 am
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.

Posted: Mon Jun 04, 2007 11:23 am
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.

Posted: Mon Jun 04, 2007 12:37 pm
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....

Posted: Mon Jun 04, 2007 12:44 pm
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 :(

Posted: Mon Jun 04, 2007 1:12 pm
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.

Posted: Mon Jun 04, 2007 2:39 pm
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.

Posted: Mon Jun 04, 2007 2:43 pm
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