How to prevent app running if precondition check fails

if (numDirEntries == 0)
{
	// terminate with error message
	cout << "no files found in image folder [" << imageFolderName << "], exiting (sorry)\n";
	// exit(); **...has no effect**
	// ofExit(); **...ditto**
}

// in debugger, code execution continues here – I don’t want that, stuff blows up without image files

Hi @amacg,

I’m not sure what numDirEntries is, or if its been initialized, but calling ofExit() in ofApp::setup() will exit the application if the condition is true. This worked for me:

// in ofApp::setup()
    int num = 0;
    if(num == 0)
    {
        std::cout << "ofExit() called" << std::endl;
        ofExit();
    }

ofImage::load() returns a bool (true if an image has loaded properly); you may be able to use it like so:

// in ofApp::setup()
    ofImage image;
    bool isImageLoaded = image.load("image.jpg");
    if (!isImageLoaded)
    {
        std::cout << "ofExit() called" << std::endl;
        ofExit();
    }

If you’d like to load images after ofApp::setup(), you can use an ofThread (or ofxThreadedImageLoader) to do this without pausing the application.

I am using VS2017 on Windows10 Pro. I wonder why ofExit() would work for you, but not for me? Are you using a different compiler/OS? When I step through the code with the debugger, it just continues on past the line with “ofExit()” as if it was a no-op (or “pass” in Python).

I came up with a solution that works for me:

first I had:

if (!chexOK)
return;
// more code here

that didn’t work; the “more code here” didn’t run, but the app started.
next I tried:

if (!chexOK)
ofExit();
// more code here

that didn’t work; the app didn’t start, but the “more code here” part ran.
finally I tried:

if (!chexOK)
ofExit();
else
{ // more code here }

which gave the result I wanted.

1 Like

… so I guess my comment “has no effect” was technically false; it did prevent the app from starting up; but “ofExit” is a little bit of a misnomer, it does not actually “exit” the running program right there and then, so to say. Execution continues past the “ofExit()” call. At least, within the setup() method it seems to work like that.

Hey @amacg, I did a bit more testing after you posted, and yeah you’re right in that ofExit() does not immediately terminate the application. So it works, but not immediately. And if you need an immediate exit then it doesn’t really work well for control, if that makes sense.

If I call ofExit() in ofApp::setup(), the app never launches a window, but ofApp::update() and ofApp::draw() run about 60 times or so. If I call ofExit() in ofApp::update(), the remaining code in ofApp::update() finishes , and ofApp::draw() runs one final time too, but then that’s it.

I could be wrong, but I think oF has to have a chance to check and unwind some stuff (memory, listeners, close the window, etc) before it exits. I like the way you solved it though with the if-else approach. Sometimes an empty directory or an unallocated ofImage can cause problems like you said. So checks and controls are a great way to avoid crashes and undefined behavior.

1 Like