openFrameworks icon indicating copy to clipboard operation
openFrameworks copied to clipboard

Crash on exit in programmable renderer when using ofLight

Open admsyn opened this issue 12 years ago • 28 comments

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

admsyn avatar Aug 24 '14 15:08 admsyn

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);
}

admsyn avatar Aug 24 '14 16:08 admsyn

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

admsyn avatar Aug 24 '14 16:08 admsyn

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

arturoc avatar Aug 24 '14 17:08 arturoc

Just a +1 -- I've seen this sporadically too but haven't been able to figure out where it is coming from ...

bakercp avatar Aug 24 '14 17:08 bakercp

Has this occurred on other platforms or OS X only?

ghost avatar Aug 25 '14 00:08 ghost

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);
}

nongio-zz avatar Aug 29 '14 11:08 nongio-zz

You should update to the latest version,this problem has solved.

QiangJi avatar Aug 29 '14 11:08 QiangJi

maybe I'm doing something wrong... but I have just pulled from master and I confirm that the bug is still there

nongio-zz avatar Aug 29 '14 11:08 nongio-zz

@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 avatar Aug 29 '14 14:08 admsyn

@admsyn the osx freeze due to the graphics card switch makes sense, I'll try gfxCardStatus

nongio-zz avatar Aug 29 '14 15:08 nongio-zz

@admsyn does this crash happen when you force the 750m?

Ahbee avatar Aug 31 '14 14:08 Ahbee

@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.

admsyn avatar Aug 31 '14 15:08 admsyn

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 avatar Sep 01 '14 14:09 tgfrerer

@tgfrerer do you get issues with auto gpu switching turned on? If so, see #3056, could be related?

bilderbuchi avatar Sep 01 '14 14:09 bilderbuchi

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 ...

tgfrerer avatar Sep 03 '14 08:09 tgfrerer

@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

  1. shared_ptr<ofBaseRenderer> renderer;
  2. shared_ptr<ofBaseApp> OFSAptr;
  3. vector<weak_ptr<ofLight::Data> > & ofLightsData();

the order of destruction on my computer is

  1. shared_ptr<ofBaseRenderer> renderer;
  2. shared_ptr<ofBaseApp> OFSAptr;
  3. 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

  1. vector<weak_ptr<ofLight::Data> > & ofLightsData();
  2. shared_ptr<ofBaseApp> OFSAptr;
  3. 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

Ahbee avatar Sep 04 '14 18:09 Ahbee

@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

Ahbee avatar Sep 06 '14 19:09 Ahbee

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...

tgfrerer avatar Sep 06 '14 20:09 tgfrerer

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.

Ahbee avatar Sep 06 '14 20:09 Ahbee

pinging @arturoc as the resident expert on static init fiascos ;-)

bilderbuchi avatar Sep 06 '14 20:09 bilderbuchi