Re: The D Programming Language
PeteK wrote:
James Kanze wrote:
PeteK wrote:
James Kanze wrote:
PeteK wrote:
My experience is that almost no classes have a logical owner.
Most classes which have a deterministic lifetime manage that
lifetime themselves. When I'm using garbage collection in C++,
I'd guess that about 90% of the deletes are "delete this".
Which is where our experience differs. For me, most classes are
either
on the stack, in a collection or embedded in another class.
In terms of number of classes, you're probably right. In terms
of number of objects, however, it's less sure. Things like the
characters in a string, for example, can't be on the stack,
since they have variable size.
No, but they *are* in a collection. A string is just an ordered
collection of some character type. (and with small string optimisation
they often *can* be on the stack).
You seem to have missed the point. Whether they're in a
collection or not, they have to be managed. Either deep copied,
at extra expense, or with some sort of reference counting, at
extra complexity. With garbage collection, the simple memory
array IS the collection.
Only a small number of classes require the use of smart
pointers and in the end even these are bound to the lifetime
of a single class created to run the application. The word
"delete" usually only turns up in comments.
Or in a few special classes, such as smart pointers?
Nope. I use intrusive reference counting, so the only delete is in the
root base class.
I use intrusive reference counting too, when I don't have
garbage collection. It's a lot of extra logic, and it doesn't
work well in a multithreaded environment (where you want smart
pointers for other reasons). And of course, there are a lot of
cases where it doesn't work, due to cycles. And in at least one
application, where performance considerations required removing
it from a large number of pointers in the transaction. (In
fact, it's been my experience that reference counting doesn't
work very well with transaction management. Once the object has
been inserted into the transaction, the transaction has to
manage it, not some abstract smart pointer.)
Anyhow I don't see what this changes. What's the difference if the
delete is in the smart pointer, or in the required base class?
[snip]
In C++ something is logically dead when it's destructor is
called. The fact that you've got live pointers to it doesn't
mean it's alive, it means you've probably got an error in your
program.
By definition, at the language level. C++ requires us to create
a deterministic lifetime for all objects, even if the design
doesn't require it. That's what I'm arguing against.
But the logical lifetime of an object is not necessarily dependent on
the language you use. For example, you talked about using dispose in
Java. When you call dispose the object is logically dead, regardless
of whether of not you have other references to the object. If you
simply forgot to call dispose the object would still be logically dead
at the point, you just wouldn't have formally killed it off.
Certainly. Anytime you have to do something explicitly, there
is a possibility of error. Neither garbage collection nor smart
pointers can help here.
The difference, of course, is that garbage collection means that
for most objects---all those without an explicit lifetime---you
don't have something explicit to do.
[...]
True, but in practice, you don't have to worry about objects
which aren't logically dead, but which are inaccessible, because
the program doesn't need them any more.
Unless these objects contain precious resources.
Which in practice they don't (except memory). How can a value
contain a resource?
How can a value be an object? An object can have a value, but I don't
see how a value can have an object.
A value must be stored somewhere. It's not an object in the OO
sense, but it's an object in the C++ sense, i.e. it occupies
memory.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientie objet/
Beratung in objektorientierter Datenverarbeitung
9 place Simard, 78210 St.-Cyr-l'Icole, France, +33 (0)1 30 23 00 34
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]