Boost logo

Boost Users :

From: Eric Damphousse (ericdamphousse_at_[hidden])
Date: 2004-03-23 10:20:34


again, hi all....

After writing an example to demonstrate the problem, as per Peter's
request, I could not repro it, until I added this extra line
glutMainLoop(); before the return 0; of main.cpp. As for those how might
not know that glutMainLoop does.... Enters the GLUT event processing
loop, never to return. One would use the GLUT library when writing
OpenGL applications. If I comment out this line, then I can account for
every single class destructor call, i.e. no leaks. When it is NOT
commented, then most of the class destructor calls do not get called, or
printed (this I will verify) and therefore my guess is that there are
leaks. Anyway, since I have done the work to write the test case, I
might as well post it. This code does NOT contain the glutMainLoop() I
mentionned. I could do so if required, but Id need to also send you a
makefile.

When I figure out what is going on, I will let you know... If anyone has
any ideas, please feel free to share!
Thanks, Eric

Test code.....

/*****************************************************************************/
// main.cpp
/*****************************************************************************/

#include <iostream>
#include <string>

#include <boost/graph/property_iter_range.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/property_map.hpp>
#include <boost/smart_ptr.hpp>

using namespace std;
using namespace boost;

/*****************************************************************************/
//
class CObject
{
protected:
 CObject(){};
   
protected:
 virtual ~CObject(){cout << "CObject cleanup" << endl;};
   
public:
 virtual void SetObjectName(const string& strName) = 0;
 virtual const std::string GetObjectName() = 0;
};

//
class C1 : public CObject
{
public:
 C1(){};

protected:
 virtual ~C1(){cout << "C1 cleanup" << endl;};
 
public:
 // Copy constructor
 C1(const C1& object) : CObject(object){};
 
};

class C2 : public C1
{
public:
 C2();
 C2(const string& strName) : m_strName(strName) {};

 ~C2(){cout << "C2 cleanup" << endl;};
 
public:
 // Copy constructor
 C2(const C2& object) : C1(object), m_strName(object.m_strName) {};
 
// Data
private:
 string m_strName;
 
public:
 inline void SetObjectName(const string& strName) {m_strName = strName;};
 inline const std::string GetObjectName() {return m_strName;};
};

//
struct graph_node_t
{
 enum { num = 1000 };
 typedef boost::vertex_property_tag kind;
};

struct Print
{
 template<class T> Print& operator() (const T& t)
 {
  std::cout << t << endl;
  return (*this);
 }
};

//
class CTester
{
public:
 CTester() {};
 ~CTester() {cout << "CTester cleanup" << endl;};
       
// Graph stuff
private:
 typedef boost::property<boost::vertex_name_t, std::string> node_name_t;
 typedef boost::property<graph_node_t, boost::shared_ptr<CObject>,
node_name_t> node_property_t;
 typedef boost::adjacency_list<listS, vecS, bidirectionalS,
node_property_t> graph_t;

 typedef graph_traits<graph_t>::vertex_descriptor vertexdescriptor_t;

 graph_t m_Graph;

//
private:
 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> 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;
 }

//
public:
 template <typename T> void addObject(const T& object)
 {
  // Create a vertex
  boost::shared_ptr<T> spObject(new T(object));

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

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

  return def;
 }

 void PrintObjects()
 {
  graph_property_iter_range<graph_t, vertex_name_t>::type test =
get_property_iter_range(m_Graph, vertex_name_t());

  cout << "Printing all vertices:" << endl;
  std::for_each(test.first, test.second, Print());
 }
};
/*****************************************************************************/
int main(int argc, char **argv)
{
 CTester tester_class;

 tester_class.addObject(C2("class one"));
 tester_class.addObject(C2("class two"));

 boost::shared_ptr<C2> abc = tester_class.getobject<C2>("class one");
 cout << abc->GetObjectName() << endl;

 return 0;
}
/*****************************************************************************/

Peter Dimov wrote:

>Eric Damphousse wrote:
>
>
>>>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 [...]
>>>
>>>
>
>Please post a complete example that demonstrates the problem.
>_______________________________________________
>Boost-users mailing list
>Boost-users_at_[hidden]
>http://lists.boost.org/mailman/listinfo.cgi/boost-users
>
>
>



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