im using visual studio 2003, and im using the following code to detect my memory leaks:

// Memory leak information
#ifdef _DEBUG
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#include <crtdbg.h>
#else
#define DEBUG_NEW new
#endif

Here is a class that i use to call GLTextString, its still leaking.  I changed the code in GLTextString to look like this: osgText::Text* GetText(void) { return m_Text.get(); } and it still leaks.  In the following code lines like:

osg::ref_ptr<osg::ShapeDrawable> drawable = new osg::ShapeDrawable(cylinder.get(), hints.get());

also leak!  So im completely lost with osg::ref_ptr thing now.  Again my sleeves are empty! :)

////////////////////////////////////
//TwoDAxis.h
///////////////////////////////////
TwoDAxis::TwoDAxis(double size, string upLabel, string rightLabel)
{
    osg::ref_ptr<osg::Geode> geode = new osg::Geode();

    osg::ref_ptr<osg::StateSet> stateset = new osg::StateSet();
    stateset->setMode(GL_LIGHTING, osg::StateAttribute::ON);
    stateset->setMode(GL_BLEND, osg::StateAttribute::ON);
    stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);

    geode->setStateSet(stateset.get());

    osg::ref_ptr<osg::TessellationHints> hints = new osg::TessellationHints;
    hints->setDetailRatio(0.5f);

    float coneRadius = size * 0.12;
    float coneHeight = size * 0.12;

    float cylinderRadius = size * 0.05;

    // right axis
    osg::Vec3 right(size, 0, 0);

    osg::ref_ptr<osg::Cylinder> cylinder = new osg::Cylinder(right/2.0f, cylinderRadius, size);
    cylinder->setRotation(osg::Quat(M_PI_2, osg::Vec3d(0, 1, 0)));
    osg::ref_ptr<osg::ShapeDrawable> drawable = new osg::ShapeDrawable(cylinder.get(), hints.get());
    drawable->setColor(Colors::m_AxisRightColor);
    geode->addDrawable(drawable.get());

    osg::ref_ptr<osg::Cone> cone = new osg::Cone(right, coneRadius, coneHeight);
    cone->setRotation(osg::Quat(M_PI_2, osg::Vec3d(0, 1, 0)));
    drawable = new osg::ShapeDrawable(cone.get(), hints.get());
    drawable->setColor(Colors::m_AxisRightColor);
    geode->addDrawable(drawable.get());

    osg::ref_ptr<GLTextString> rightText = new GLTextString(rightLabel, size * 0.3);
    rightText->GetText()->setAutoRotateToScreen(true);
    rightText->GetText()->setPosition(right + osg::Vec3(8, 0, 0));
    geode->addDrawable(rightText->GetText());

    // up axis
    osg::Vec3 up(0, size, 0);

    cylinder = new osg::Cylinder(up/2.0f, cylinderRadius, size);
    cylinder->setRotation(osg::Quat(M_PI_2, osg::Vec3d(1, 0, 0)));
    drawable = new osg::ShapeDrawable(cylinder.get(), hints.get());
    drawable->setColor(Colors::m_AxisUpColor);
    geode->addDrawable(drawable.get());

    cone = new osg::Cone(up, coneRadius, coneHeight);
    cone->setRotation(osg::Quat(-M_PI_2, osg::Vec3d(1, 0, 0)));
    drawable = new osg::ShapeDrawable(cone.get(), hints.get());
    drawable->setColor(Colors::m_AxisUpColor);
    geode->addDrawable(drawable.get());

    osg::ref_ptr<GLTextString> upText = new GLTextString(upLabel, size * 0.3);
    upText->GetText()->setAutoRotateToScreen(true);
    upText->GetText()->setPosition(up + osg::Vec3(0, 8, 0));
    geode->addDrawable(upText->GetText());

    // Node
    osg::ref_ptr<osg::Sphere> sphere = new osg::Sphere(osg::Vec3(0, 0, 0), size * 0.1);
    drawable = new osg::ShapeDrawable(sphere.get(), hints.get());
    drawable->setColor(Colors::m_AxisNodeColor);
    geode->addDrawable(drawable.get());

    addChild(geode.get());
}


Thrall, Bryan wrote:
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()
{
}
    

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

Reply via email to