Hi Mikhail,

I have moved the static into the global scope of the
osgWrappers/deprecated-dotosg/Drawable.cpp, file attached, could try
it out?

Robert.

On 2 June 2012 11:12, Mikhail I. Izmestev <[email protected]> wrote:
> Hi,
>
> I noticed crashing in dotosg wrappers when try to use osgDB::readNodeFile
> from multiple threads.
> Crash occurs when I try to load .osg file from two threads.
>
> For example callstack of crashed thread:
> osg80_osgDB!concrete_wrapper::matches+0x4
> osg80_osgDB!osgDB::DeprecatedDotOsgWrapperManager::readObjectOfType+0x527
> osg80_osgDB!osgDB::DeprecatedDotOsgWrapperManager::readObjectOfType+0x28
> osgdb_deprecated_osg!Drawable_readLocalData+0xa1
> osg80_osgDB!osgDB::DeprecatedDotOsgWrapperManager::readObject+0x8fb
> osg80_osgDB!osgDB::DeprecatedDotOsgWrapperManager::readDrawable+0xe1
> osgdb_deprecated_osg!Geode_readLocalData+0x90
> osg80_osgDB!osgDB::DeprecatedDotOsgWrapperManager::readObject+0x8fb
> osg80_osgDB!osgDB::DeprecatedDotOsgWrapperManager::readNode+0xe1
> osgdb_deprecated_osg!Group_readLocalData+0x71
> osg80_osgDB!osgDB::DeprecatedDotOsgWrapperManager::readObject+0x8fb
> osg80_osgDB!osgDB::DeprecatedDotOsgWrapperManager::readNode+0xe1
> osgdb_deprecated_osg!Group_readLocalData+0x71
> osg80_osgDB!osgDB::DeprecatedDotOsgWrapperManager::readObject+0x8fb
> osg80_osgDB!osgDB::DeprecatedDotOsgWrapperManager::readNode+0xe1
> osgdb_osg!OSGReaderWriter::readNode+0xfe
> osgdb_osg!OSGReaderWriter::readNode+0x31c
> osg80_osgDB!osgDB::Registry::ReadNodeFunctor::doRead+0x28
> osg80_osgDB!osgDB::Registry::read+0x5e4
> osg80_osgDB!osgDB::Registry::readImplementation+0x34c
> osg80_osgDB!osgDB::Registry::readNodeImplementation+0x6a
> osg80_osgDB!osgDB::Registry::readNode+0xfc
> osg80_osgDB!osgDB::readNodeFile+0x4e
> [...]
>
> second thread callstack:
> msvcp90!std::basic_istream<char,std::char_traits<char>
> msvcp90!std::basic_istream<char,std::char_traits<char>
> osg80_osgDB!osgDB::FieldReader::_readField+0x3f8
> osg80_osgDB!osgDB::FieldReaderIterator::field+0x19d
> osgdb_deprecated_osg!Array_readLocalData+0xbfb
> osgdb_deprecated_osg!Geometry_readLocalData+0x991
> osg80_osgDB!osgDB::DeprecatedDotOsgWrapperManager::readObject+0x8fb
> osg80_osgDB!osgDB::DeprecatedDotOsgWrapperManager::readDrawable+0xe1
> osgdb_deprecated_osg!Geode_readLocalData+0x90
> osg80_osgDB!osgDB::DeprecatedDotOsgWrapperManager::readObject+0x8fb
> osg80_osgDB!osgDB::DeprecatedDotOsgWrapperManager::readNode+0xe1
> osgdb_deprecated_osg!Group_readLocalData+0x71
> osg80_osgDB!osgDB::DeprecatedDotOsgWrapperManager::readObject+0x8fb
> osg80_osgDB!osgDB::DeprecatedDotOsgWrapperManager::readNode+0xe1
> osgdb_deprecated_osg!Group_readLocalData+0x9a
> osg80_osgDB!osgDB::DeprecatedDotOsgWrapperManager::readObject+0x8fb
> osg80_osgDB!osgDB::DeprecatedDotOsgWrapperManager::readNode+0xe1
> osgdb_deprecated_osg!Group_readLocalData+0x9a
> osg80_osgDB!osgDB::DeprecatedDotOsgWrapperManager::readObject+0x8fb
> osg80_osgDB!osgDB::DeprecatedDotOsgWrapperManager::readNode+0xe1
> osgdb_deprecated_osg!Group_readLocalData+0x71
> osg80_osgDB!osgDB::DeprecatedDotOsgWrapperManager::readObject+0x8fb
> osg80_osgDB!osgDB::DeprecatedDotOsgWrapperManager::readNode+0xe1
> osgdb_osg!OSGReaderWriter::readNode+0xfe
> osgdb_osg!OSGReaderWriter::readNode+0x31c
> osg80_osgDB!osgDB::Registry::ReadNodeFunctor::doRead+0x28
> osg80_osgDB!osgDB::Registry::read+0x5e4
> osg80_osgDB!osgDB::Registry::readImplementation+0x34c
> osg80_osgDB!osgDB::Registry::readNodeImplementation+0x6a
> osg80_osgDB!osgDB::Registry::readNode+0xfc
> osg80_osgDB!osgDB::readNodeFile+0x4e
> [...]
>
> code:
> struct concrete_wrapper: basic_type_wrapper
> {
>    virtual ~concrete_wrapper() {}
>    concrete_wrapper(const osg::Object *myobj) : myobj_(myobj) {}
>    bool matches(const osg::Object *proto) const
>    {
>        return myobj_->isSameKindAs(proto);  <<<<<<<<<<<<<<<<<CRASH
> HERE<<<<<<<
>    }
>    const osg::Object *myobj_;
> };
>
> crash occurs because myobj_ is NULL.
>
> myobj_ passed from osgdb_deprecated_osg!Drawable_readLocalData as
> s_drawstate
>
> bool Drawable_readLocalData(Object& obj, Input& fr)
> {
>    bool iteratorAdvanced = false;
>
>    Drawable& drawable = static_cast<Drawable&>(obj);
>
>    static ref_ptr<StateSet> s_drawstate = new osg::StateSet;
>    if (StateSet* readState =
> static_cast<StateSet*>(fr.readObjectOfType(*s_drawstate)))
>    {
>        drawable.setStateSet(readState);
>        iteratorAdvanced = true;
>    }
> [...]
>
>
> so problem is in
>    static ref_ptr<StateSet> s_drawstate = new osg::StateSet;
>
> MSVC 2008 x64 compiler (maybe gcc too) generate this code as:
>
> static bool s_drawstate_init = false;
> static ref_ptr<StateSet> s_drawstate = NULL;
>
> bool Drawable_readLocalData(Object& obj, Input& fr)
> {
>    bool iteratorAdvanced = false;
>
>    Drawable& drawable = static_cast<Drawable&>(obj);
>
>    if(!s_drawstate_init)
>    {
>        s_drawstate_init = true;
>        s_drawstate = new osg::StateSet;
>    }
>
>    if (StateSet* readState =
> static_cast<StateSet*>(fr.readObjectOfType(*s_drawstate)))
>    {
>        drawable.setStateSet(readState);
>        iteratorAdvanced = true;
>    }
> [...]
>
>
> and this code is not thread safe as you can see.
>
> Possible solution is to move static variables to global scope to avoid lazy
> initialization.
>
>
> Mikhail.
> _______________________________________________
> osg-users mailing list
> [email protected]
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
#include "osg/Drawable"
#include "osg/Notify"

#include "osgDB/Registry"
#include "osgDB/Input"
#include "osgDB/Output"

using namespace osg;
using namespace osgDB;

// forward declare functions to use later.
bool Drawable_readLocalData(Object& obj, Input& fr);
bool Drawable_writeLocalData(const Object& obj, Output& fw);

// register the read and write functions with the osgDB::Registry.
REGISTER_DOTOSGWRAPPER(Drawable)
(   NULL,
    "Drawable",
    "Object Drawable",
    &Drawable_readLocalData,
    &Drawable_writeLocalData
);

static ref_ptr<StateSet> s_drawstate = new osg::StateSet;

bool Drawable_readLocalData(Object& obj, Input& fr)
{
    bool iteratorAdvanced = false;

    Drawable& drawable = static_cast<Drawable&>(obj);

    if (StateSet* readState = static_cast<StateSet*>(fr.readObjectOfType(*s_drawstate)))
    {
        drawable.setStateSet(readState);
        iteratorAdvanced = true;
    }

    Shape* shape = static_cast<Shape *>(fr.readObjectOfType(type_wrapper<Shape>()));
    if (shape)
    {
        drawable.setShape(shape);
        iteratorAdvanced = true;
    }

    Drawable::UpdateCallback* uc = dynamic_cast<Drawable::UpdateCallback *>(fr.readObjectOfType(type_wrapper<Drawable::UpdateCallback>()));
    if (uc)
    {
        drawable.setUpdateCallback(uc);
        iteratorAdvanced = true;
    }

    Drawable::CullCallback* cc = dynamic_cast<Drawable::CullCallback *>(fr.readObjectOfType(type_wrapper<Drawable::CullCallback>()));
    if (cc)
    {
        drawable.setCullCallback(cc);
        iteratorAdvanced = true;
    }

    Drawable::DrawCallback* dc = dynamic_cast<Drawable::DrawCallback *>(fr.readObjectOfType(type_wrapper<Drawable::DrawCallback>()));
    if (dc)
    {
        drawable.setDrawCallback(dc);
        iteratorAdvanced = true;
    }

    if (fr.matchSequence("initialBound %f %f %f %f %f %f"))
    {
        BoundingBox bb;
        fr[1].getFloat(bb.xMin());
        fr[2].getFloat(bb.yMin());
        fr[3].getFloat(bb.zMin());
        fr[4].getFloat(bb.xMax());
        fr[5].getFloat(bb.yMax());
        fr[6].getFloat(bb.zMax());
        drawable.setInitialBound(bb);
        fr += 7;
        iteratorAdvanced = true;
    }

    Drawable::ComputeBoundingBoxCallback* cbc = dynamic_cast<Drawable::ComputeBoundingBoxCallback *>(fr.readObjectOfType(type_wrapper<Drawable::ComputeBoundingBoxCallback>()));
    if (cbc)
    {
        drawable.setComputeBoundingBoxCallback(cbc);
        iteratorAdvanced = true;
    }

    if (fr[0].matchWord("supportsDisplayList"))
    {
        if (fr[1].matchWord("TRUE"))
        {
            drawable.setSupportsDisplayList(true);
            fr+=2;
            iteratorAdvanced = true;
        }
        else if (fr[1].matchWord("FALSE"))
        {
            drawable.setSupportsDisplayList(false);
            fr+=2;
            iteratorAdvanced = true;
        }
    }

    if (fr[0].matchWord("useDisplayList"))
    {
        if (fr[1].matchWord("TRUE"))
        {
            drawable.setUseDisplayList(true);
            fr+=2;
            iteratorAdvanced = true;
        }
        else if (fr[1].matchWord("FALSE"))
        {
            drawable.setUseDisplayList(false);
            fr+=2;
            iteratorAdvanced = true;
        }
    }

    if (fr[0].matchWord("useVertexBufferObjects"))
    {
        if (fr[1].matchWord("TRUE"))
        {
            drawable.setUseVertexBufferObjects(true);
            fr+=2;
            iteratorAdvanced = true;
        }
        else if (fr[1].matchWord("FALSE"))
        {
            drawable.setUseVertexBufferObjects(false);
            fr+=2;
            iteratorAdvanced = true;
        }
    }

    return iteratorAdvanced;
}


bool Drawable_writeLocalData(const Object& obj, Output& fw)
{
    const Drawable& drawable = static_cast<const Drawable&>(obj);

    if (drawable.getStateSet())
    {
        fw.writeObject(*drawable.getStateSet());
    }

    if (drawable.getShape())
    {
        fw.writeObject(*drawable.getShape());
    }

    if (drawable.getUpdateCallback())
    {
        fw.writeObject(*drawable.getUpdateCallback());
    }

    if (drawable.getEventCallback())
    {
        fw.writeObject(*drawable.getEventCallback());
    }

    if (drawable.getCullCallback())
    {
        fw.writeObject(*drawable.getCullCallback());
    }

    if (drawable.getDrawCallback())
    {
        fw.writeObject(*drawable.getDrawCallback());
    }


    if (drawable.getInitialBound().valid())
    {
        const osg::BoundingBox& bb = drawable.getInitialBound();
        fw.indent()<<"initialBound "<<bb.xMin()<<" "<<bb.yMin()<<" "<<bb.zMin()<<" "
                                    <<bb.xMax()<<" "<<bb.yMax()<<" "<<bb.zMax()<<std::endl;
    }

    if (drawable.getComputeBoundingBoxCallback())
    {
        fw.writeObject(*drawable.getComputeBoundingBoxCallback());
    }


    if (!drawable.getSupportsDisplayList())
    {
        fw.indent()<<"supportsDisplayList ";
        if (drawable.getSupportsDisplayList()) fw << "TRUE" << std::endl;
        else fw << "FALSE" << std::endl;
    }

    fw.indent()<<"useDisplayList ";
    if (drawable.getUseDisplayList()) fw << "TRUE" << std::endl;
    else fw << "FALSE" << std::endl;

    fw.indent()<<"useVertexBufferObjects ";
    if (drawable.getUseVertexBufferObjects()) fw << "TRUE" << std::endl;
    else fw << "FALSE" << std::endl;

    return true;
}
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to