hello!
the short, mechanical answer is scope: by the time you call ofRunApp()
, the ofAppNoWindow window
does not exist anymore (it exists only within the scope of your f()
function).
this lead to a more subtle question: why does that matter? because of hidden “side-effects” within ofSetupOpenGL()
: in there, a pointer is constructed out of the address &window
. Then, ofRunApp()
“wirelessly” expect that invisible-to-you pointer to point to an actual window (but by then it’s gone). This sort of “wireless” behaviour is a sign of fragile design (as you just revealed) as you are implicitly expected to maintain the lifetime of your window beyond where you can “see” it used.
detecting bad pointers is hard for the system, but at the segfault, your debugger should have oriented you in the vicinity of line 73 of ofMainLoop.cpp:
ofAddListener(window->events().setup,app.get(),&ofBaseApp::setup,OF_EVENT_ORDER_APP);
it may not spell out “you are referencing a window that does not exist” (it’s hard to deduce), but it’s telling you something in there is the cause of the crash (in your case, ->events()
does not exist because window
is invalid).
all that being said you must additionally wonder: “but I’m not interested in the window, I’m using ofAppNoWindow
!” heh you should certainly not need a window! but considering the origins and use of OF, the window is pretty deeply ingrained, and the noWindow is still a window.
note that there is another pattern of bootstrapping OF that looks like this (it should be what a fresh ProjectGenerated project provides):
#include "ofMain.h"
#include "ofApp.h"
int main( ) {
ofGLWindowSettings settings;
settings.setSize(1024, 768);
settings.windowMode = OF_WINDOW; //can also be OF_FULLSCREEN
auto window = ofCreateWindow(settings);
ofRunApp(window, make_shared<ofApp>());
ofRunMainLoop();
}
here the relationship is made apparent, as the window is explicitly passed to ofRunApp. better design, as you would not have been tempted to separate the auto window
line from the ofRunApp()
one. (and incidentally it’s making use of ofGLWindowSettings
which provides more flexibility than the direct interface to ofSetupOpenGL()
).