From: Szepesvari Csaba (szepes_at_[hidden])
Date: 1999-07-24 08:14:44
> Greg Colvin wrote:
> > Nonetheless, live_ptr looks useful, so I'd like to see if it can be done with minimal
> > intrusion. I don't consider requiring derivation from live_ptee to be minimal.
> In C++, there is no non-intrusive way to detect a call to a
> function, for any function (a normal function or a destructor
> or operator delete).
> The obvious intrusive way is to modify the function foo to
> dispatch a foo_called message.
I have a related question: How would you use all these smart pointers in
multithreaded environments? (It's not a provocation but a very practical
issue - some kind of automatic garbage collection is usually essential in
multi-threaded programs e.g. dealing with GUIs.)
Here is the situation I am thinking about: you have a refcounted object
rco which is referenced in object a1. Imagine further that thread 1 is
calling into one of rco's method through the reference in object a1 which
starts some long lasting computation. Now thread 2 somehow arrives at a1
and decides to discard the reference to object rco which happens to be the
very last reference to object rco. Therefore the reference counting
mechanism decides to delete object rco - an object which is "computing"
something. Afterwards devil things may happen. E.g. the reference
to the virtual table that lived in object rco may be reused for some other
purpose and if thread 1 tries to call a virtual function of object rco
then the system will probably crash.
This is not akin to C++, of course you can simulate objects and virtual
method tables in C.
The only way out that I can imagine is to protect every call into the
object and also every parameter passing (i.e. adjust the reference count
every time to call into the object or when you pass the object as a
parameter). But this would require quite a bit of additional work..
I think that the conservative garbage collector of Boehm would also be
fooled by the above code - after discarding the reference to object
rco in object a1 there are no real references to object rco anywhere in
the program so the Boehm collector would collect it. I am not sure about
Great Circle, I am not familiar with that collector.
- Csaba Szepesvari
Csaba Szepesvari, PhD
Konkoly-Thege Miklos u. 29-33.
Phone: +36 1 395 9220/1205 (dial extension continuously)
Fax: +36 1 395 9218
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk