[osg-users] SetAttribute / RemoveAttribute Question

Robert Osfield robert.osfield at gmail.com
Tue Nov 28 08:46:17 PST 2017


Hi Paul,

You haven't provided a stack trace, or how you manage your nodes and
pointers so there isn't much we can say specifically.

As a general comment, these type of crashes on what should otherwise
be a perfect reasonable operation, is usually down to dangling
pointers of some kind.  In your case perhaps the node has been deleted
elsewhere so your node_unsel pointer is invalid, or the pointer to the
material is invalid.

If you are keeping pointers around to objects in the scene graph you
shouldn't use C pointers as they can easily become dangling pointers.
If you do need to keep a pointer around either use an osg::ref_ptr<>
to make sure you keep the object alive even when all other references
to it are removed, or use an osg::observer_ptr<> that automatically
gets reset to 0 when the object it pointers is deleted.

Robert.


On 28 November 2017 at 16:35, Paul Bigeon <osgforum at tevs.eu> wrote:
> Hi,
>
> Considering I've created an osg::Material this way, and that the nodes are shown on screen :
>
>
> Code:
>
> m_material_selected = new osg::Material();
> m_material_selected->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4f(0.2f, 0.2f, 0.75f, 0.5f));
> m_material_selected->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4f(0.2f, 0.2f, 0.75f, 0.5f));
> m_material_selected->setShininess(osg::Material::FRONT_AND_BACK, 75.0);
> m_material_selected->setColorMode(osg::Material::AMBIENT);
>
>
>
>
> And then I set it to a node like this ( this is apparently working so far ... ) :
>
>
> Code:
>
> // Where node_sel is a previously created osg::Node*
> node_sel->getOrCreateStateSet()->setAttribute(m_material_selected, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
>
>
>
>
> But when I try to remove this attribute from the previously created node like this :
>
>
> Code:
>
> // Where node_unsel = node_sel;
> node_unsel->getOrCreateStateSet()->removeAttribute(m_material_selected);
>
>
>
>
> My app crashes.
>
> What did I do wrong ? Is it unsafe to removeAttribute from a node this way ?
> I've seen on the forum things like NodeVisitor class, should I be using this or can I change nodes manually ?
>
>
> Thank you for your interest in my question,
>
> Have a good day,
>
> Paul
>
> ------------------
> Read this topic online here:
> http://forum.openscenegraph.org/viewtopic.php?p=72479#72479
>
>
>
>
>
> _______________________________________________
> osg-users mailing list
> osg-users at lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


More information about the osg-users mailing list