Hello everyone! :]

I am trying to redirect all OSG notifications to a file by creating my own  
notify handler class (inheriting from osg::NotifyHandler) but I get some random 
crushes. Mostly during run-time and sometimes when I close the application. All 
of them reporting some kind of heap corruption. Sometimes it might take about 
5-10 minutes in order to crush. Here is my code:

Code:

class LogFileHandler : public osg::NotifyHandler
{
public:
    LogFileHandler(const std::string& filename) {
        _log.open( filename.c_str() );
        _validFile = _log.is_open();
    }
    void notify( osg::NotifySeverity severity, const char* message ) {
        OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
        if(!_validFile) return;
        _log << message;
    }
protected:
    ~LogFileHandler(void) {
        _log.close();
    }
protected:
    std::ofstream _log;
    OpenThreads::Mutex _mutex;
    bool _validFile;
};



In order to test this class I set the handler like this:
osg::setNotifyHandler( new LogFileHandler("log.txt") );

Why do I get crushes reporting an invalid heap? Am I doing something wrong?
OSG notes:
"Note that osg notification API is not thread safe although notification  
handler is called from many threads. When incorporating handlers into GUI 
widgets you must take care of thread safety on your own."
Isn't the ScopedLock I have at the top of my notify() function enough?

If I set the osgViewer threading mode to SingleThreaded, the problem goes away. 
This makes me believe that my problem has something to do with multithreading.
I am using OSG 3.0.0 and Visual Studio 2008 SP1.

Thanks a lot for your time.

Cheers,
George

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=44063#44063





_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to