|
Boost Users : |
From: Pavel Antokolsky aka Zigmar (zigmar_at_[hidden])
Date: 2005-04-29 01:30:48
On 4/29/05, Philippe Mori <philippe_mori_at_[hidden]> wrote:
> >
> > And another pitfal with both auto_ptr and shared_ptr (at least on
> > VC7.1 compiler) is if you don't provide expicit destructor, the
> > generated destructor will try destroy the auto_ptr, which in its turn
> > will try to destroy you pimpl via imcomplete pointer, therefor not
> > calling destructor. Fortunately compiler at least gives a warning:
> > "deletion of pointer to incomplete type 'TestClass::PrivateData'; no
> > destructor called"
> >
>
> I suppose that a complete type is required to instanciate shared_ptr
> destructor and any function that will destroy the pointee.
>
> I was thinking that shared_ptr does ensure by itself that a complete
> type is used (contrary to auto_ptr) so that the type must be complete
> to compile properly. Do you just have a warning meaning that on
> older compiler someone might not even notice the problem.
>
> IMO, the fact that shared_ptr accept incomplete type but
> require a complete type is what make it interesting for pimpl.
>
> Philippe
I've re-checked the issue with incomplete type, and I see I was wrong.
For such class (class without destructor explicitly instantiated):
class TestClass
{
public:
TestClass();
// Note there is no destructor
// virtual ~TestClass(void);
TestClass(const TestClass& other);
TestClass& operator=(const TestClass& other);
private:
class PrivateData;
// std::auto_ptr<PrivateData> pd;
// boost::scoped_ptr<PrivateData> pd;
boost::shared_ptr<PrivateData> pd;
};
The results was:
auto_ptr - compiled with warning about destroying incomplete type
scoped_ptr - failed to compile (thanks to "checked_delete")
shared_ptr - compiled and worked - the destructor of
TestClass::PrivateData was called (don't know how it works).
-- Best regards, Zigmar
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