Proper handling of exit()

I’ve been having trubble exiting the app and keeping everything in place. I’ve read all the related posts but still can’t figure out how to exit nicely.

My main problem comes because I need to log when the user exits. I have a class listening to the exit event and then it makes an http post request to log the last info.

I’ve got all sorts of errors. Most of the times in totally unrelated threaded classes and with a similar log to this:

malloc: *** error for object 0x1f7c3a4: incorrect checksum for freed object - object was probably modified after being freed.

I think I’m not quite understanding how to hanadle exit properly.

Simple example

void ofApp::exit()
{
    ofSleepMillis(1000);
    cout << "\nBYE!";
}

Most of the times some threaded function crashes before “BYE!” is displayed. So, I deduce that threaded functions are stopped before ofApp::exit() ?

Event if a threaded class is listening to the exit to dispose it properly like this…

void threadedDecoder::handleExit(ofEventArgs &e)
{
    if(isThreadRunning())
        waitForThread();
}

… a lot of times crashes before the listening function gets call.

Some times I get badaccess trying to access the local time with:

NSString *currentTime = [dateFormatter stringFromDate:today];

GLFW it doesn’t seems to be available either, so I’m unable to get the user window size when she’s quitting for example (this doesn’t crash but ofGetWindowWidth() returns 0)

So, how ofApp::exit() actually works? Is the kind of operation I’m trying a valid use case? How a correct implementation should look like?
What stuff still available and what is already disposed before its call?
Also, how a threaded class should handle it? And how can I prioritize an specific class exit call?

It seems ofApp::exit() its called before the exit event is triggered. So, I should be able to do this to ensure that at least my log is sent before some other classes crashes:

void ofApp::exit()
{
	analytics.exit();
}

But for some reason this seems to make crashes even more frequent.

Hope someone can clarify this. Thanks! :wink:

there’s a bug in the way the exit function and event are called in 0.8.4, this is fixed in github but there’s no easy way to avoid it in 0.8.4. the problem is that some times the destructor is being called before the exit method. an option is to not use exit at all and instead do everything in the destructor of each class

Thanks @arturo. Good to know.
How far is 0.9.0 to release? Not sure if I should check it already or wait a little bit. Is it stable?