Boost logo

Boost Users :

From: Eric Damphousse (ericdamphousse_at_[hidden])
Date: 2004-03-21 16:26:52


hi all,
quick question concerning shared_ptr... not sure if things get deleted
(destroyed) properly and its freaking me out. I hope this is not too long

Basically, I use the BGL to store my data and that seems to be ok. The
way the BGL stuff is defined is as such... Note that spObjectNode is
defined as typedef boost::shared_ptr<CObject> spObjectNode

 // Set vertex properties
 typedef boost::property<boost::vertex_name_t, std::string> node_name_t;
 typedef boost::property<graph_node_t, spObjectNode, node_name_t>
node_property_t;
 
 // Set edge properties
 typedef boost::property<boost::edge_name_t, std::string> connection_name_t;
 typedef boost::property<graph_connection_t, spObjectNode,
connection_name_t> edge_property_t;

 // Set graph
 typedef adjacency_list<listS, vecS, bidirectionalS, node_property_t,
edge_property_t> graph_t;

//
graph_t m_Graph;

The function below is what I use to add a node to the BGL...
template <typename T> void AddNode(const T& object)
{
  // Create the vertex
  boost::shared_ptr<T> pNode(new T(object));

  // Add the vertex + its name to the graph
  vertexdescriptor_t newNode = boost::add_vertex(node_property_t(pNode,
node_name_t(pNode->GetObjectName())), m_Graph);
};

The way I use the AddNode ... for example in a main.cpp:

/// blablabla above
CSphere sphere("sphere1");
CSceneDB::Instance().AddNode(sphere);
return 0;

For testing purposes, in the base class and derived classes (see below)
I print a message in the destructor of the class, just to make sure
things are ok.
If I just have in my main the previous call then upon exit I expect and
get the following message...

CSceneDB cleanup
CSphere cleanup
CGeometry cleanup
CObject cleanup

Now for the source of my anxiety...
I wrote a function to get back my object from the BGL and it goes like
this...

inline boost::shared_ptr<CObject> getNodeProperty(const
vertexdescriptor_t& vertex)
 {
     boost::property_map<graph_t, graph_node_t>::type node_prop_map;
     node_prop_map = boost::get(graph_node_t(), m_Graph);
     
     return node_prop_map[vertex];
 }
 
boost::shared_ptr<CObject> CSceneDB::getNodebyName(const std::string&
strName)
{
  boost::shared_ptr<CObject> node_prop;

 boost::graph_traits<graph_t>::vertex_iterator vi, vi_end;
 boost::tie(vi, vi_end) = boost::vertices(m_Graph);

 for(; vi != vi_end; ++vi)
 {
  node_prop = getNodeProperty(*vi);

  if(node_prop != 0)
  {
   if(node_prop->GetObjectName() == strName)
             break;
  }
 }
 
 return node_prop;
}

template <typename T> boost::shared_ptr<T> getNode(const std::string&
strName)
 {
   boost::shared_ptr<CObject> abc = getNodebyName(strName);
  shared_ptr<T> def = boost::dynamic_pointer_cast<T>(abc);

  return def;
 }

The problem is that if I do something like this... in the main.cpp

/// blablabla stuff above
CSphere sphere("sphere1");
CSceneDB::Instance().AddNode(sphere);

boost::shared_ptr<CSphere> abc =
CSceneDB::Instance().getNode<CSphere>("sphere1");
return 0;

I only get upon exit...

CSceneDB cleanup

what happened to CSphere cleanup, CGeometry cleanup and CObject cleanup,
are they leaking?

Now for a sanity check, if I do in the main.cpp ... i do NOT do the
assignment boost::shared_ptr<CSphere> abc =

/// blablabla stuff above
CSphere sphere("sphere1");
CSceneDB::Instance().AddNode(sphere);

CSceneDB::Instance().getNode<CSphere>("sphere1");
return 0;

upon exit i get ...

CSceneDB cleanup
CSphere cleanup
CGeometry cleanup
CObject cleanup

Can someone tell me why this is, is my assignment wrong? Any help would
be greatly appreciated
Eric

Brief description of the classes used ofr my test

// Object.h
class CObject
{
protected:
    CObject(){};
   
protected:
    virtual ~CObject(){cout << "CObject cleanup" << endl;};
};

typedef boost::shared_ptr<CObject> spObjectNode;

// Geometry.h
class CGeometry : public CObject
{
protected:
    CGeometry(){};
   
protected:
    virtual ~CGeometry(){cout << "CGeometry cleanup" << endl;};
};

// Sphere.h
class CSphere : public CGeometry
{
public:
    CSphere(){};
    ~CSphere(){cout << "CSphere cleanup" << endl;};
};



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net