1

I tried adding a fragment shader program to OSG::Geometry node as below.

    osg::ref_ptr<osg::Geometry> node = new osg::Geometry();
    osg::ref_ptr<osg::Program> m_program= new osg::Program;
    osg::ref_ptr<osg::Shader> fragShader = new osg::Shader(osg::Shader::FRAGMENT);
    //TODO Add LOG if shader failed to load
    if (!fragShader->loadShaderSourceFromFile("Shaders/Sel.frag"))
        return;
    m_program->addShader(fragShader);
    osg::StateSet* state = node->getOrCreateStateSet();
    state->setAttributeAndModes(m_program, osg::StateAttribute::ON);

At some point, I removed the program using the below method

state->removeAttribute(m_program);

After removing the attribute, the next immediate render frame loop frame() throws an exception as below.

enter image description here

I tried to debug the openscenegraph and found the map which is causing the issue.

Header file: State

Method name :

inline void State::applyAttributeList(AttributeMap& attributeMap,const StateSet::AttributeList& attributeList)

The variable which caused the exception.

attributeMap

if I am not removing the program state attribute, it is working fine. Only removing the attribute, causing the issue.

genpfault
  • 51,148
  • 11
  • 85
  • 139
S.Frank Richarrd
  • 488
  • 1
  • 3
  • 15
  • Could you show the code for the definition of `applyAttributeList`? The issue probably occurs inside that method, but we're unable to see what it actually does/observe where your bug might be. – Xirema Aug 15 '18 at 15:34
  • Unrelated: That's not an exception, not in the C++ sense. That's a runtime assert. Generalizing a bit, assert is a macro that performs a test and terminates the program if the test fails. A C++ exception you can catch, handle, and keep the program running. You can't catch the assert. Typically when you release the code to the user, the assert is compiled differently to do nothing, not even the test so there is no performance impact. it only exists for debugging to catch mistakes and force you to fix them before users get their hands on it. – user4581301 Aug 15 '18 at 15:47
  • The error message suggests that you have an iterator that has been rendered invalid. If you get an iterator for a container and then modify the container, odds are good ([See here for details](https://stackoverflow.com/questions/6438086/iterator-invalidation-rules)) that the container's moved stuff around and your iterator no longer references what it should. – user4581301 Aug 15 '18 at 15:52
  • @Xirema it is an API method from Openscenegraph graphics library – S.Frank Richarrd Aug 15 '18 at 16:17

1 Answers1

1

This is most likely due to the fact that you're updating the stateset while the rendering thread(s) is still using it to finish the dispatch of the previous frame. To make sure this is the cause of the crash, you can try to run the application with OSG SingleThreaded scheme (either set it via code on the viewer or set the OSG_THREADING env var).
If that is the cause and you want to use the one of the other threading schemes, you can set the stateset's data variance to DYNAMIC - this will ensure that the rendering thread is done with your stateset before the next update callback is called for the new frame.
This topic has been discussed many times in the osg-users mailing list, you may check the archives for further info.

rickyviking
  • 846
  • 6
  • 17
  • Thanks for the answer. It helped a lot. I checked with the SingleThreaded scheme, it is working fine. So I switched back to automaticthreading model and tried changing stateset's data variance. It is still crashing. But it is fixed after setting setDataVariance(osg::Object::DYNAMIC); to osg::Geometry node. – S.Frank Richarrd Aug 20 '18 at 18:56