|
Boost Users : |
From: Per Kristensen (per.kristensen_at_[hidden])
Date: 2002-11-15 03:13:38
You initialises two shared_ptr with the same raw pointer. As a consequent the destructor is called twice, as you will have two different shared_count object which will calls delete as the count become zero.
Try to use weak_ptr as a member:
-----------------------------------
typedef shared_ptr<MyClass> MyClassPtr;
class MyClass
{
public:
MyClass();
~MyClass();
void setOriginal(MyClassPtr);
MyClassPtr makeCopy();
weak_ptr<MyClass> original;
};
MyClass::MyClass()
{
cout << "constructor called " << this << endl;
}
MyClass::~MyClass()
{
cout << "destructor called " << this << endl;
}
void MyClass::setOriginal(MyClassPtr p)
{
original = p;
}
MyClassPtr MyClass::makeCopy()
{
return MyClassPtr(original);
}
main()
{
MyClassPtr oc(new MyClass);
oc->setOriginal(oc);
MyClassPtr cc = oc->makeCopy();
}
---------------------------------
Per
-----Original Message-----
From: Stephen Crowley [mailto:stephenc_at_[hidden]]
Sent: 15. november 2002 05:37
To: Boost-Users_at_[hidden]
Subject: [Boost-Users] using 'this' with shared_ptrs
How is someone supposed to use 'this' when setting a member of another class
to point back to it?
Here is an example.. when you run it, this will happen.
constructor called 0x804af10
constructor called 0x804af48
destructor called 0x804af48
destructor called 0x804af10
destructor called 0x804af10
The last destructor is called twice! The only way I can think to get around
this add a member to the class called "shared_ptr<MyClass> thisptr" which is
initialized to "this" in the constructor and over-ride the assignment
operator or something.. will that work?
------------------------
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <iostream>
using namespace std;
using namespace boost;
class MyClass;
typedef shared_ptr<MyClass> MyClassPtr;
class MyClass
{
public:
MyClass();
~MyClass();
MyClassPtr makeCopy();
MyClassPtr original;
};
MyClass::MyClass()
{
cout << "constructor called " << this << endl;
}
MyClass::~MyClass()
{
cout << "destructor called " << this << endl;
}
MyClassPtr MyClass::makeCopy()
{
MyClassPtr mc(new MyClass);
mc->original = MyClassPtr(this);
return mc;
}
main()
{
MyClassPtr oc(new MyClass);
MyClassPtr cc = oc->makeCopy();
// the problem lies here
// when cc's reference count is 0 it will be deleted, which will
// in turn decrease its 'original' reference count which will be 0, thus
// causing it to be deleted as well even though it is still being used by oc!
cc.reset();
}
-----------------------------------
-- Stephen Info: < http://www.boost.org> Wiki: < http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl> Unsubscribe: <mailto:boost-users-unsubscribe_at_[hidden]> Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service <http://docs.yahoo.com/info/terms/> . [Non-text portions of this message have been removed]
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