[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

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
::rectangle_with_profile( boost::shared_ptr 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
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
::rectangle_with_profile( boost::shared_ptr 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
m_rectangle is private of rectangle_with_profile
and defined as boost::shared_ptr 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
::rectangle_with_profile( boost::shared_ptr 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
..."<
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
..."<
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