Boost logo

Boost :

From: E. Gladyshev (egladysh_at_[hidden])
Date: 2002-03-25 01:57:18


>
> The classic approach for people who care about this
> is to use
> "intrusive" shared pointers, where the reference
> count is stored inside
> the pointed-to object.

It works in many cases but not always.

>Typically, the
> only reason you are
> forced to pass around a plain pointer is some legacy
> code or system
> programming interface you can't control, and even
> that is pretty rare.

I don't think that it is pretty rare. If you worked
on huge and multi-team projects, then I am sure
you know that inter-module API's are specified
in C much more often than in C++. It is especially
important if the project has many DLL's or some of
the client code has to be C.
It is never a wise solution to export C++
classes from DLL's (remember name mangling...etc.)
Let's get practical :).

> Keeping a std::map of all shared pointers would
> enable all sorts of
> features and advanced error checking. But its cost
> in performance is
> prohibitive, even if you ignore multi-threading
> issues. An
> implementation that works properly in multi-threaded
> applications would
> have an even greater cost.

I never said that my idea is going to solve all
the problems. It just might work better for certain
application that is all. I think that
it is pretty pointless to discuss how prohibitive
the performance cost is without a specific context.

>
> > The following code shows how to do that.
>
> The "test code" you provided would not work properly
> in multi-threaded
> application.

Of course not, sorry I forgot to point it out
in my originaly e-mail.
This is why it is just a test code.
But I believe that it's very obvious
how to make the code thread safe, is not it?

> The latest version of boost::shared_ptr can handle
> this. When you
> construct a shared_ptr, you can pass in a function
> that is used to
> destroy the object instead of the default "delete".
> Here's one way to do
> it.
>
> #include <boost/shared_ptr.hpp>
> void do_nothing(int*) { }
> int g_x;
> int main()
> {
> boost::shared_ptr<int> tmp(&g_x,
> do_nothing);
> }

No, I don't think your suggestion will work
with my example because foo() will have to
know to use do_nothing() as well, but
it has not idea what deleter to use.
Just try it for yourself and remember
we are looking for a generic solution.

>
> Once you've had some experience with smart pointers,
> you'll find that
> it's best to have very few interfaces that use plain
> pointers, so the
> first problem you mentioned becomes a non-problem.

Unfortunately our experience has nothing
to do with the real life requirements.
In most cases we don't have the total control
over how the project interfaces are defined.
I am sure you are well aware of that.

__________________________________________________
Do You Yahoo!?
Yahoo! Movies - coverage of the 74th Academy Awards®
http://movies.yahoo.com/


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk