[shared_ptr] A newbie boost::shared_ptr question

Hi all, I am trying to use shared_ptr in my code, but, I encoutered some problems. Here is a piece of my code : The main : boost::shared_ptr<OGRRectangle> rect( new OGRRectangle(5.,TPoint2D <double>(50.,20.),0.1325) ); boost::shared_ptr< rectangle_with_profile<OGRRectangle> > r( new rectangle_with_profile<OGRRectangle> ( rect , 5 , 12 , 1.5 , EXTREME) ); The rectangle_with_profile<T> constructor : template <class Rect_class> rectangle_with_profile<Rect_class>::rectangle_with_profile( boost::shared_ptr<Rect_class> rect , const unsigned int &nb_profiles , const unsigned int &nb_points , const double &distance , const eProfilePosition &position=CENTER ) { m_rectangle = rect; // ... // ... } Just after the line boost::shared_ptr< rectangle_with_profile<OGRRectangle> > r( new rectangle_with_profile<OGRRectangle> ( rect , 5 , 12 , 1.5 , EXTREME) ); the rect<T> destructor is called, and the m_rectangle in rectangle_with_profile handles a deleted pointer ... What am I missing ? Thanks in advance for your help. -- Le temps des cerises reviendra. Dans l'immédiat, c'est le temps des noyaux. Courage.

Olivier Tournaire wrote:
Hi all,
I am trying to use shared_ptr in my code, but, I encoutered some problems. Here is a piece of my code :
The main :
boost::shared_ptr<OGRRectangle> rect( new OGRRectangle(5.,TPoint2D <double>(50.,20.),0.1325) ); boost::shared_ptr< rectangle_with_profile<OGRRectangle> > r( new rectangle_with_profile<OGRRectangle> ( rect , 5 , 12 , 1.5 , EXTREME) );
The rectangle_with_profile<T> constructor :
template <class Rect_class> rectangle_with_profile<Rect_class>::rectangle_with_profile( boost::shared_ptr<Rect_class> rect , const unsigned int &nb_profiles , const unsigned int &nb_points , const double &distance , const eProfilePosition &position=CENTER ) { m_rectangle = rect; // ... // ... }
What is the Type of m_rectangle? It should be m_rectangle? If it isnt, this is maybe the problem (And it is of type scoped_ptr e.g.) Because than maybe the scoped_ptr would call the destructor. Could you post some more quellcode? Greetings Manuel Jung
Just after the line boost::shared_ptr< rectangle_with_profile<OGRRectangle> > r( new rectangle_with_profile<OGRRectangle> ( rect , 5 , 12 , 1.5 , EXTREME) ); the rect<T> destructor is called, and the m_rectangle in rectangle_with_profile handles a deleted pointer ...
What am I missing ?
Thanks in advance for your help.

m_rectangle is private of rectangle_with_profile<Rect_class> and defined as boost::shared_ptr<Rect_class> m_rectangle; Thanks 2007/5/8, Manuel Jung <gzahl@arcor.de>:
Olivier Tournaire wrote:
Hi all,
I am trying to use shared_ptr in my code, but, I encoutered some problems. Here is a piece of my code :
The main :
boost::shared_ptr<OGRRectangle> rect( new OGRRectangle(5.,TPoint2D <double>(50.,20.),0.1325) ); boost::shared_ptr< rectangle_with_profile<OGRRectangle> > r( new rectangle_with_profile<OGRRectangle> ( rect , 5 , 12 , 1.5 , EXTREME) );
The rectangle_with_profile<T> constructor :
template <class Rect_class> rectangle_with_profile<Rect_class>::rectangle_with_profile( boost::shared_ptr<Rect_class> rect , const unsigned int &nb_profiles , const unsigned int &nb_points , const double &distance , const eProfilePosition &position=CENTER ) { m_rectangle = rect; // ... // ... }
What is the Type of m_rectangle? It should be m_rectangle? If it isnt, this is maybe the problem (And it is of type scoped_ptr e.g.) Because than maybe the scoped_ptr would call the destructor. Could you post some more quellcode? Greetings Manuel Jung
Just after the line boost::shared_ptr< rectangle_with_profile<OGRRectangle> > r( new rectangle_with_profile<OGRRectangle> ( rect , 5 , 12 , 1.5 , EXTREME) ); the rect<T> destructor is called, and the m_rectangle in rectangle_with_profile handles a deleted pointer ...
What am I missing ?
Thanks in advance for your help.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Le temps des cerises reviendra. Dans l'immédiat, c'est le temps des noyaux. Courage.

No one can help me ? 2007/5/8, Olivier Tournaire <olitour@gmail.com>:
m_rectangle is private of rectangle_with_profile<Rect_class> and defined as boost::shared_ptr<Rect_class> m_rectangle;
Thanks
2007/5/8, Manuel Jung < gzahl@arcor.de>:
Olivier Tournaire wrote:
Hi all,
I am trying to use shared_ptr in my code, but, I encoutered some problems. Here is a piece of my code :
The main :
boost::shared_ptr<OGRRectangle> rect( new OGRRectangle(5.,TPoint2D <double>(50.,20.),0.1325) ); boost::shared_ptr< rectangle_with_profile<OGRRectangle> > r( new rectangle_with_profile<OGRRectangle> ( rect , 5 , 12 , 1.5 , EXTREME) );
The rectangle_with_profile<T> constructor :
template <class Rect_class> rectangle_with_profile<Rect_class>::rectangle_with_profile( boost::shared_ptr<Rect_class> rect , const unsigned int &nb_profiles ,
const unsigned int &nb_points , const double &distance , const eProfilePosition &position=CENTER ) { m_rectangle = rect; // ... // ... }
What is the Type of m_rectangle? It should be m_rectangle? If it isnt, this is maybe the problem (And it is of type scoped_ptr e.g.) Because than maybe the scoped_ptr would call the destructor. Could you post some more quellcode? Greetings Manuel Jung
Just after the line boost::shared_ptr< rectangle_with_profile<OGRRectangle> > r( new rectangle_with_profile<OGRRectangle> ( rect , 5 , 12 , 1.5 , EXTREME) ); the rect<T> destructor is called, and the m_rectangle in rectangle_with_profile handles a deleted pointer ...
What am I missing ?
Thanks in advance for your help.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Le temps des cerises reviendra. Dans l'immédiat, c'est le temps des noyaux. Courage.
-- Le temps des cerises reviendra. Dans l'immédiat, c'est le temps des noyaux. Courage.

OK. Here is the incriminated part of the main : void Main_MPP( void ) { boost::shared_ptr<OGRRectangle> rect( new OGRRectangle(5.,TPoint2D <double>(50.,20.),0.1325) ); boost::shared_ptr< rectangle_with_profile<OGRRectangle> > r( new rectangle_with_profile<OGRRectangle> ( rect , 5 , 12 , 1.5 , EXTREME) ); //OGRRectangle destructor is call here ... r->DisplayConsole(); } Here is the OGRRectangle class : class OGRRectangle : public OGRPolygon { public: OGRRectangle() {}; OGRRectangle( const double &width , const TPoint2D <double> ¢er , const double &angle=0. ); OGRRectangle( const TPoint2D <double> ¢er , const double &width , const double &height , const double &angle=0. ); OGRRectangle( const TPoint2D <double> &TopLeft , const TPoint2D <double> &BottomRight , const double &angle=0. ); ~OGRRectangle() {SORTIEMESSAGE("Destruction OGRRectangle ..."<<std::endl);}; void Init( const TPoint2D <double> ¢er , const double &width , const double &height , const double &angle=0. ); inline void GetWidth( double &width ) {width=m_width;} inline double GetWidth() {return m_width;} inline void GetHeight( double &height ) {height=m_height;} inline double GetHeight() {return m_height;} void Translate( const TPoint2D <double> &trans ); void Translate( const double &x , const double &y ); void Rotate( const double &angle ); protected: double m_width; double m_height; double m_angle; }; // Implementation void OGRRectangle::Init( const TPoint2D <double> ¢er , const double &width , const double &height , const double &angle ) { m_width = width; m_height = height; m_angle = angle; boost::shared_ptr<OGRPoint> pt( new OGRPoint ); boost::shared_ptr< OGRLinearRing > lr( new OGRLinearRing ); pt->setCoordinateDimension(2); pt->setX( center.x-0.5*width ); pt->setY( center.y-0.5*height ); pt->setZ( 0. ); lr->addPoint( pt.get() ); pt->setX( center.x+0.5*width ); pt->setY( center.y-0.5*height ); lr->addPoint( pt.get() ); pt->setX( center.x+0.5*width ); pt->setY( center.y+0.5*height ); lr->addPoint( pt.get() ); pt->setX( center.x-0.5*width ); pt->setY( center.y+0.5*height ); lr->addPoint( pt.get() ); this->addRing( lr.get() ); lr->setCoordinateDimension(2); this->setCoordinateDimension(2); this->closeRings(); if ( m_angle != 0. ) Rotate(m_angle); } OGRRectangle::OGRRectangle( const double &width , const TPoint2D <double> ¢er , const double &angle ) { Init(center,width,width,angle); setCoordinateDimension(2); } OGRRectangle::OGRRectangle( const TPoint2D <double> ¢er , const double &width , const double &height , const double &angle) { Init(center,width,height,angle); setCoordinateDimension(2); } OGRRectangle::OGRRectangle( const TPoint2D <double> &TopLeft , const TPoint2D <double> &BottomRight , const double &angle) { double width = abs(BottomRight.x - TopLeft.x); double height = abs(BottomRight.y - TopLeft.y); Init(0.5*(TopLeft+BottomRight),width,height,angle); setCoordinateDimension(2); } // rectangle_with_profile template <class Rect_class> class rectangle_with_profile { friend class profile; public: rectangle_with_profile() {}; rectangle_with_profile( boost::shared_ptr<Rect_class> rect , const unsigned int &nb_profiles , const unsigned int &nb_points , const double &distance , const eProfilePosition &position=CENTER ); ~rectangle_with_profile() {}; inline void SetRectangle( boost::shared_ptr<Rect_class> rect ) {m_rectangle = rect;} inline boost::shared_ptr<Rect_class> GetRectangle() {return m_rectangle;} void SetProfiles( const std::vector< boost::shared_ptr< profile<Rect_class> > > &profiles ) {m_profiles=profiles;} void AddProfile( const TPoint2D <double> &first , const unsigned int &nb_points , const double &distance ); void AddProfile( const TPoint2D <double> &first , const TPoint2D <double> &last , const unsigned int &nb_points ); inline void GetProfiles( std::vector< boost::shared_ptr< profile<Rect_class> > > &profiles ) {profiles=m_profiles;} inline std::vector< boost::shared_ptr< profile<Rect_class> > > GetProfiles() {return m_profiles;} void GetProfile( boost::shared_ptr< profile<Rect_class> > &profiles , const unsigned int &i ); boost::shared_ptr< profile<Rect_class> > GetProfile(const unsigned int &i); void DrawShape( IdCalqueVecteur &layer , const bool &summits=true , const bool &edges=true , const bool ¢er=false , const bool &profiles=true ); void DrawAttributes( IdCalqueVecteur &layer , const bool &angle=true , const bool &width=true , const bool &height=true ); void DisplayConsole(); void Translate( const TPoint2D <double> &trans ); void Translate( const double &x , const double &y ); void Rotate( const double &angle ); private: std::vector< boost::shared_ptr< profile<Rect_class> > > m_profiles; boost::shared_ptr<Rect_class> m_rectangle; }; // its constructor template <class Rect_class> rectangle_with_profile<Rect_class>::rectangle_with_profile( boost::shared_ptr<Rect_class> rect , const unsigned int &nb_profiles , const unsigned int &nb_points , const double &distance , const eProfilePosition &position=CENTER ) : m_rectangle(rect) { m_rectangle = rect; TPoint2D <double> h_direction, w_direction, profile_center; if ( typeid(m_rectangle.get()) == typeid(rectangle*) ) { boost::shared_ptr<rectangle> rect( (rectangle*)(m_rectangle.get()) ); h_direction = rect->GetRectangle()[1] - rect->GetRectangle()[0]; w_direction = rect->GetRectangle()[1] - rect->GetRectangle()[2]; profile_center.setx( 0.5 * ( rect->GetRectangle()[0].x + rect->GetRectangle()[3].x ) ); profile_center.sety( 0.5 * ( rect->GetRectangle()[0].y + rect->GetRectangle()[3].y ) ); TPoint2D <double> normalized_h_direction = h_direction; TPoint2D <double> normalized_w_direction = w_direction; normalized_h_direction.normaliser(); normalized_w_direction.normaliser(); unsigned int i; switch( position ) { case CENTER: { double length_inter = h_direction.norme() / (double)(nb_profiles+1); for (i=1;i<=nb_profiles;i++) { // Point central du profil profile_center = profile_center + ( (double)i * length_inter * normalized_h_direction ); TPoint2D <double> first; first.x = profile_center.x + (int)(0.5*nb_points) * distance * normalized_w_direction.x; first.y = profile_center.y + (int)(0.5*nb_points) * distance * normalized_w_direction.y; AddProfile(first,nb_points,distance); } } break; case EXTREME: { double length_inter = h_direction.norme() / (double)(nb_profiles-1); for (i=0;i<nb_profiles;i++) { // Point central du profil boost::shared_ptr<rectangle> rect( reinterpret_cast<rectangle*>(m_rectangle.get()) ); TPoint2D <double> profile_center( 0.5 * ( rect->GetRectangle()[0] + rect->GetRectangle()[3] ) ); profile_center = profile_center + ( (double)i * length_inter * normalized_h_direction ); TPoint2D <double> first; first.x = profile_center.x + (int)(0.5*nb_points) * distance * normalized_w_direction.x; first.y = profile_center.y + (int)(0.5*nb_points) * distance * normalized_w_direction.y; AddProfile(first,nb_points,distance); } } break; default: break; } } else if ( typeid(m_rectangle.get()) == typeid(OGRRectangle*) ) { boost::shared_ptr<OGRRectangle> ogr_rect( (OGRRectangle*)(m_rectangle.get()) ); OGRLinearRing *cur_ring = ogr_rect->getExteriorRing(); int nb = cur_ring->getNumPoints(); boost::shared_ptr< OGRRawPoint > struc( new OGRRawPoint[ nb ] ); cur_ring->getPoints( struc.get() ); h_direction.x = (struc.get())[1].x - (struc.get())[0].x; h_direction.y = (struc.get())[1].y - (struc.get())[0].y; w_direction.x = (struc.get())[1].x - (struc.get())[2].x; w_direction.y = (struc.get())[1].y - (struc.get())[2].y; profile_center.setx( 0.5 * ( (struc.get())[0].x + (struc.get())[3].x ) ); profile_center.sety( 0.5 * ( (struc.get())[0].y + (struc.get())[3].y ) ); TPoint2D <double> normalized_h_direction = h_direction; TPoint2D <double> normalized_w_direction = w_direction; normalized_h_direction.normaliser(); normalized_w_direction.normaliser(); unsigned int i; switch( position ) { case CENTER: { double length_inter = h_direction.norme() / (double)(nb_profiles+1); for (i=1;i<=nb_profiles;i++) { // Point central du profil profile_center = profile_center + ( (double)i * length_inter * normalized_h_direction ); TPoint2D <double> first; first.x = profile_center.x + (int)(0.5*nb_points) * distance * normalized_w_direction.x; first.y = profile_center.y + (int)(0.5*nb_points) * distance * normalized_w_direction.y; AddProfile(first,nb_points,distance); } } break; case EXTREME: { double length_inter = h_direction.norme() / (double)(nb_profiles-1); for (i=0;i<nb_profiles;i++) { TPoint2D <double> profile_center; profile_center.setx( 0.5 * ( (struc.get())[0].x + ( struc.get())[3].x ) ); profile_center.sety( 0.5 * ( (struc.get())[0].y + ( struc.get())[3].y ) ); profile_center = profile_center + ( (double)i * length_inter * normalized_h_direction ); TPoint2D <double> first; first.x = profile_center.x + (int)(0.5*nb_points) * distance * normalized_w_direction.x; first.y = profile_center.y + (int)(0.5*nb_points) * distance * normalized_w_direction.y; AddProfile(first,nb_points,distance); } } break; default: break; } } } Hope you could help me. Regards. 2007/5/9, Peter Dimov <pdimov@mmltd.net>:
Olivier Tournaire wrote:
No one can help me ?
Can you post a complete program that shows the error? I see nothing wrong with your code snippets.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Le temps des cerises reviendra. Dans l'immédiat, c'est le temps des noyaux. Courage.

Olivier Tournaire wrote:
if ( typeid(m_rectangle.get()) == typeid(rectangle*) ) { boost::shared_ptr<rectangle> rect( (rectangle*)(m_rectangle.get()) );
This is the cause of your immediate problem. You are creating a second shared_ptr to the same raw pointer. This second shared_ptr has no knowledge of m_rectangle and will call delete on the raw pointer when it goes out of scope. Why not just use m_rectangle inside the if? You don't need the cast or the second shared_ptr.

Great, that was it. Best regards. Peter Dimov a écrit :
Olivier Tournaire wrote:
if ( typeid(m_rectangle.get()) == typeid(rectangle*) ) { boost::shared_ptr<rectangle> rect( (rectangle*)(m_rectangle.get()) );
This is the cause of your immediate problem. You are creating a second shared_ptr to the same raw pointer. This second shared_ptr has no knowledge of m_rectangle and will call delete on the raw pointer when it goes out of scope. Why not just use m_rectangle inside the if? You don't need the cast or the second shared_ptr.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

OK. Here is the incriminated part of the main : void Main_MPP( void ) { boost::shared_ptr<OGRRectangle> rect( new OGRRectangle(5.,TPoint2D <double>(50.,20.),0.1325) ); boost::shared_ptr< rectangle_with_profile<OGRRectangle> > r( new rectangle_with_profile<OGRRectangle> ( rect , 5 , 12 , 1.5 , EXTREME) ); //OGRRectangle destructor is call here ... r->DisplayConsole(); } Here is the OGRRectangle class : class OGRRectangle : public OGRPolygon { public: OGRRectangle() {}; OGRRectangle( const double &width , const TPoint2D <double> ¢er , const double &angle=0. ); OGRRectangle( const TPoint2D <double> ¢er , const double &width , const double &height , const double &angle=0. ); OGRRectangle( const TPoint2D <double> &TopLeft , const TPoint2D <double> &BottomRight , const double &angle=0. ); ~OGRRectangle() {SORTIEMESSAGE("Destruction OGRRectangle ..."<<std::endl);}; void Init( const TPoint2D <double> ¢er , const double &width , const double &height , const double &angle=0. ); inline void GetWidth( double &width ) {width=m_width;} inline double GetWidth() {return m_width;} inline void GetHeight( double &height ) {height=m_height;} inline double GetHeight() {return m_height;} void Translate( const TPoint2D <double> &trans ); void Translate( const double &x , const double &y ); void Rotate( const double &angle ); protected: double m_width; double m_height; double m_angle; }; // Implementation void OGRRectangle::Init( const TPoint2D <double> ¢er , const double &width , const double &height , const double &angle ) { m_width = width; m_height = height; m_angle = angle; boost::shared_ptr<OGRPoint> pt( new OGRPoint ); boost::shared_ptr< OGRLinearRing > lr( new OGRLinearRing ); pt->setCoordinateDimension(2); pt->setX( center.x-0.5*width ); pt->setY( center.y-0.5*height ); pt->setZ( 0. ); lr->addPoint( pt.get() ); pt->setX( center.x+0.5*width ); pt->setY( center.y-0.5*height ); lr->addPoint( pt.get () ); pt->setX( center.x+0.5*width ); pt->setY( center.y+0.5*height ); lr->addPoint( pt.get() ); pt->setX( center.x-0.5*width ); pt->setY( center.y+0.5*height ); lr->addPoint( pt.get() ); this->addRing( lr.get() ); lr->setCoordinateDimension(2); this->setCoordinateDimension(2); this->closeRings(); if ( m_angle != 0. ) Rotate(m_angle); } OGRRectangle::OGRRectangle( const double &width , const TPoint2D <double> ¢er , const double &angle ) { Init(center,width,width,angle); setCoordinateDimension(2); } OGRRectangle::OGRRectangle( const TPoint2D <double> ¢er , const double &width , const double &height , const double &angle) { Init(center,width,height,angle); setCoordinateDimension(2); } OGRRectangle::OGRRectangle( const TPoint2D <double> &TopLeft , const TPoint2D <double> &BottomRight , const double &angle) { double width = abs(BottomRight.x - TopLeft.x); double height = abs(BottomRight.y - TopLeft.y); Init(0.5*(TopLeft+BottomRight),width,height,angle); setCoordinateDimension(2); } 2007/5/9, Peter Dimov < pdimov@mmltd.net>:
Olivier Tournaire wrote:
No one can help me ?
Can you post a complete program that shows the error? I see nothing wrong with your code snippets.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Le temps des cerises reviendra. Dans l'immédiat, c'est le temps des noyaux. Courage.
participants (3)
-
Manuel Jung
-
Olivier Tournaire
-
Peter Dimov