B
Boris
I've a class C with a smart pointer (I use boost::shared_ptr) which is
initialized in the constructor:
class C {
boost::shared_ptr<D> d;
public:
C() : d(new d()) { }
};
When the program starts class C is instantiated quite a lot. As all the
instances of C use the same empty instance of D I want them to share the
instance:
class C {
boost::shared_ptr<D> d;
static boost::shared_ptr<D> one;
public:
C() : d(one) { }
};
boost::shared_ptr<D> C:ne(new d());
That would work if I wasn't using the quick_allocator for boost::shared_ptr
(a memory manager which is used when BOOST_SP_USE_QUICK_ALLOCATOR is
defined). Now as it turns out this memory manager uses static variables.
According to the FAQ we initialize the static member now in a method:
class C {
boost::shared_ptr<D> d;
static boost::shared_ptr<D> one;
public:
C() : d(one) { }
static Init() { one.reset(new d()); }
};
boost::shared_ptr<D> C:ne;
Now everything works until the program ends. If the static variables in the
memory manager of boost::shared_ptr are destroyed first the destructor of
the static smart pointer in class C will make a call to a non-existing
memory manager.
The only solution I see is freeing the static smart pointer when the last
instance of class C is destroyed (then noone needs the instance of D in the
smart pointer anymore). The destructor uses the counter of the smart pointer
to check when it should be freed:
class C {
boost::shared_ptr<D> d;
static boost::shared_ptr<D> one;
public:
C() : d(one) { }
static Init() { one.reset(new d()); }
~C() { if (one.unique()) one.reset(); }
};
boost::shared_ptr<D> C:ne;
This is however no general solution (eg. if you destroy all instances of C
and then create a new one in the middle of the program the smart pointer is
empty). Is there a better idea how to handle this?
Boris
initialized in the constructor:
class C {
boost::shared_ptr<D> d;
public:
C() : d(new d()) { }
};
When the program starts class C is instantiated quite a lot. As all the
instances of C use the same empty instance of D I want them to share the
instance:
class C {
boost::shared_ptr<D> d;
static boost::shared_ptr<D> one;
public:
C() : d(one) { }
};
boost::shared_ptr<D> C:ne(new d());
That would work if I wasn't using the quick_allocator for boost::shared_ptr
(a memory manager which is used when BOOST_SP_USE_QUICK_ALLOCATOR is
defined). Now as it turns out this memory manager uses static variables.
According to the FAQ we initialize the static member now in a method:
class C {
boost::shared_ptr<D> d;
static boost::shared_ptr<D> one;
public:
C() : d(one) { }
static Init() { one.reset(new d()); }
};
boost::shared_ptr<D> C:ne;
Now everything works until the program ends. If the static variables in the
memory manager of boost::shared_ptr are destroyed first the destructor of
the static smart pointer in class C will make a call to a non-existing
memory manager.
The only solution I see is freeing the static smart pointer when the last
instance of class C is destroyed (then noone needs the instance of D in the
smart pointer anymore). The destructor uses the counter of the smart pointer
to check when it should be freed:
class C {
boost::shared_ptr<D> d;
static boost::shared_ptr<D> one;
public:
C() : d(one) { }
static Init() { one.reset(new d()); }
~C() { if (one.unique()) one.reset(); }
};
boost::shared_ptr<D> C:ne;
This is however no general solution (eg. if you destroy all instances of C
and then create a new one in the middle of the program the smart pointer is
empty). Is there a better idea how to handle this?
Boris