Tamer El Nashar wrote on Monday, March 31, 2008 1:56 PM:
> Any ideas why the class from the code below would cause memory leaks?
> I've read as many tutorials as i could find, to learn the proper use
> of 
> the osg::ref_ptr, and i tried all the tricks up my sleeve.  However
> I'm still getting a memory leak in the member variable m_Text;

How are you detecting the memory leaks?

The only interesting thing about your class I see is the m_Text object
will have two references: the m_Text member, and the entry in
osg::Geode's Drawable list. However, when GLTextString goes out of
scope, both those should disappear and the osgText::Text will be
deleted...

Probably not related, but returning osg::ref_ptr<> causes extra
reference counting overhead (which can be significant if it happens a
lot, and is even worse when reference counting thread-safety is
enabled). You could just return osgText::Text*.

HTH

> ////////////////////////////////////
> //GLTextString.h
> ///////////////////////////////////
> class GLTextString : pblic osg::Geode
> {
> private:
>     osg::ref_ptr<osgText::Text> m_Text;
> 
> public:
>     static osg::Vec4 m_Color;
>     static osg::Vec4 m_BackdropColor;
> 
>     GLTextString(std::string str, float fontSize = 20.0f, std::string
> font = "fonts/tahoma.ttf");
> protected:
>     virtual ~GLTextString();
> 
> public:
>     osg::ref_ptr<osgText::Text> GetText(void) { return m_Text; }
>     void SetText(string text) { m_Text->setText(text); }
> };
> 
> ////////////////////////////////////
> //GLTextString.cpp
> ///////////////////////////////////
> 
> osg::Vec4 GLTextString::m_Color = osg::Vec4(0.8f, 0.8f, 0.8f, 1.0f);
> osg::Vec4 GLTextString::m_BackdropColor = osg::Vec4(1.0f, 1.0f, 1.0f,
> 1.0f); 
> 
> GLTextString::GLTextString(std::string str, float fontSize, string
> font) {
>     osg::Vec3 pos(0, 0, -.5 );
> 
>     m_Text = new osgText::Text;
>     m_Text->setColor(m_Color);
>     m_Text->setFont("fonts/tahoma.ttf");
>     m_Text->setCharacterSize(fontSize);
>     m_Text->setPosition(pos);
>     m_Text->setAxisAlignment(osgText::Text::SCREEN);
>     m_Text->setBackdropType(osgText::Text::DROP_SHADOW_BOTTOM_RIGHT);
>     m_Text->setBackdropColor(m_BackdropColor);
> 
>     if (!str.empty())
>         m_Text->setText(str);
> 
>     addDrawable(m_Text.get());
> 
>     // turn lighting off for the text and disable depth test to ensure
> its always ontop.
>     osg::StateSet* stateset = getOrCreateStateSet();
>     stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
> 
>     // Disable depth test, and make sure that the string is drawn
> after everything
>     // else so that it always appears on top.
>     stateset->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
> 
>     setStateSet(stateset);
> }
> 
> GLTextString::~GLTextString()
> {
> }

-- 
Bryan Thrall
FlightSafety International
[EMAIL PROTECTED]
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to