Crash on exit in programmable renderer when using ofLight
I've noticed this happening on OSX (not very often, but often enough to be annoying).
On exit, there's a crash on this line in ofGLUtils.cpp
shared_ptr<ofBaseGLRenderer> ofGetGLRenderer(){
> if(ofGetCurrentRenderer()->getType()==ofGLRenderer::TYPE || ofGetCurrentRenderer()->getType()==ofGLProgrammableRenderer::TYPE){
....
The crash happens inside the stdlib string comparison function for std::string. If I set a breakpoint in the string comparison function, it looks like it gets called with two NULL strings, but I'm not sure how reliable that is :)
Seems related to #3065
Actually, it looks like this is happens consistently if the ofApp contains an ofLight. The ofLight::Data destructor calls ofGetGLRenderer() several times, with the first call causing the crash.
ofLight::Data::~Data(){
if(glIndex==-1) return;
> ofGetGLRenderer()->setLightAmbientColor(glIndex,ofColor(0,0,0,255));
ofGetGLRenderer()->setLightDiffuseColor(glIndex,ofColor(0,0,0,255));
ofGetGLRenderer()->setLightSpecularColor(glIndex,ofColor(0,0,0,255));
ofGetGLRenderer()->setLightPosition(glIndex,ofVec4f(0,0,1,0));
ofGetGLRenderer()->disableLight(glIndex);
}
Yeah, definitely a bit of weirdness going on here :)
I tried breaking up the if(ofGetCurrentRenderer()->getType()==ofGLRenderer::TYPE ... line into parts to see if there was a way to do an early check and bail, but got some weird behaviour.
- checking the return of ofGetCurrentRenderer() for NULL doesn't catch anything
- trying to get the renderer type string in isolation creates a
std::length_error - wrapping the whole thing in a try/catch just gets an EXC_BAD_ACCESS on getType(), though sometimes the catch triggers
yeah, @NickHardeman reported this through the forum a while ago, i changed the renderer to a heap allocated shared_ptr but it doesn't seem to fix it. it's difficult to debug because it depends on the order in which files are compiled and it doesn't happen in linux, i'll try to take a look in osx
Just a +1 -- I've seen this sporadically too but haven't been able to figure out where it is coming from ...
Has this occurred on other platforms or OS X only?
I've tried to debug this issue with no luck... I've got 3 times my computer freezed on osx 10.9.4 debugging this bug (maybe is an opengl driver issue?)
what about checking if the app is quitting to avoid the glrenderer settings:
ofLight::Data::~Data(){
if(glIndex==-1) return;
if(OF_QUITTING) return;
ofGetGLRenderer()->setLightAmbientColor(glIndex,ofColor(0,0,0,255));
ofGetGLRenderer()->setLightDiffuseColor(glIndex,ofColor(0,0,0,255));
ofGetGLRenderer()->setLightSpecularColor(glIndex,ofColor(0,0,0,255));
ofGetGLRenderer()->setLightPosition(glIndex,ofVec4f(0,0,1,0));
ofGetGLRenderer()->disableLight(glIndex);
}
You should update to the latest version,this problem has solved.
maybe I'm doing something wrong... but I have just pulled from master and I confirm that the bug is still there
@QiangJi No it's still an issue, unfortunately :)
@nongio I think that's something else, actually. Hard to say, since it's a pretty vague bug in both cases.
@admsyn the osx freeze due to the graphics card switch makes sense, I'll try gfxCardStatus
@admsyn does this crash happen when you force the 750m?
@Ahbee Yes it happens regardless.
The easiest way to reproduce it is to run the 3d/3DPrimitivesExample. The crash happens 100% of the time while running that example, for me.
Just tested latest master::3d/3DPrimitivesExample OSX 10.10 / Xcode6, GeForce GT 750M (with auto graphics switching on and off), and the crash on exit seems not to happen in this configuration.
@tgfrerer do you get issues with auto gpu switching turned on? If so, see #3056, could be related?
the trouble is, i can't reproduce the issues with or without gpu switching - i was hoping to, so that i could give it a shot at fixing this ...
@admsyn . I am not sure but I think this is the problem. The first problem is that shared_ptr<ofBaseRenderer> & ofGetCurrentRenderer() should be changed to this.
shared_ptr<ofBaseRenderer> & ofGetCurrentRenderer(){
static shared_ptr<ofBaseRenderer> renderer;
return renderer;
}
instead of
shared_ptr<ofBaseRenderer> & ofGetCurrentRenderer(){
static shared_ptr<ofBaseRenderer> * renderer = new shared_ptr<ofBaseRenderer>();
return *renderer;
}
the second problem is the order of destruction of static variables.( Some computers might do this differently which is why some @tgfrerer did not get the bug)
I found 3 static variables in OF
shared_ptr<ofBaseRenderer> renderer;shared_ptr<ofBaseApp> OFSAptr;vector<weak_ptr<ofLight::Data> > & ofLightsData();
the order of destruction on my computer is
shared_ptr<ofBaseRenderer> renderer;shared_ptr<ofBaseApp> OFSAptr;vector<weak_ptr<ofLight::Data> > & ofLightsData();
which will cause errors because ofLight::Data destructor needs renderer to still be alive.
If the order of destruction is
vector<weak_ptr<ofLight::Data> > & ofLightsData();shared_ptr<ofBaseApp> OFSAptr;shared_ptr<ofBaseRenderer> renderer;
you will not get errors.
@tgfrerer can you check the order of destruction by putting print statements inside the destructors of ofApp , ofGLRenderer ,and ofLight::Data::~Data(),do this with the 3DPrimitivesExample
@admsyn does creating a global ofLight in ofApp.cpp crash your program on start.
ofLight light;
//--------------------------------------------------------------
void ofApp::setup(){
}
//--------------------------------------------------------------
I think the problem is static initialization fiasco
hm.
agree, it could be that lazy instantiation causes ambiguities in destruction order - i have seen wondrous things happen with lazy instantiations, especially in multi-threaded contexts.
Although ofLight::Data's destructor needs a renderer to still be alive, the whole idea of wrapping it into a weak_ptr is that ofLightsData does not own any of its pointed-to ofLight::Data objects. Upon destruction, it should therefore not attempt to delete any of these objects, so i think we could at least rule out that one...
In my case it looks like the renederer(the one inside ofGetCurrentRenderer()) is dead by the time ofLight::Data's destructor's is called. My question is what should get destroyed first on exit? Is it the Renderer inside ofGetCurrentRenderer() or is it the main ofApp(which is also static) ? if the renederer gets destructed first it is a problem.
pinging @arturoc as the resident expert on static init fiascos ;-)