delete and references?
Jeremie Pelletier
jeremiep at gmail.com
Sat Aug 8 10:29:08 PDT 2009
Andrei Alexandrescu Wrote:
> Michel Fortin wrote:
> > On 2009-08-08 09:17:28 -0400, Andrei Alexandrescu
> > <SeeWebsiteForEmail at erdani.org> said:
> >
> >> Great description.
> >>
> >> FWIW, I am trying to convince Walter to not reclaim memory in delete,
> >> but instead only call destructors. D continues C++'s mistake of
> >> conflating lifetime termination with memory reclamation.
> >
> > I don't see how this changes anything. Instead of accessing a
> > deallocated object, you'll access a finaized but not yet deallocated
> > object. In both cases, it's a bug.
>
> The former case has you using memory that was possibly allocated to an
> object of different type. This makes for intractable errors.
>
> > Wouldn't it be better to have a system to track unique pointers? If you
> > knew that no other pointer points to a given object or memory block, you
> > can finalize and deallocate it safely. In fact, the current semantics of
> > a scope object assume that the programmer will not leave any dangling
> > pointers at the end of the scope, so it's already assuming uniqueness,
> > just not enforcing it.
>
> Unique pops in an out as a topic for future directions. It is quite
> difficult to make it at the same time correct, inconspicuous, and
> useful. We have firmly decided to do away with unique for D2.
>
>
> Andrei
I agree, scope arguments can easily be casted away and live beyond the scope. It's impossible to know if a pointer is unique without scanning the entire heap and pausing all threads to scan their stacks. And adding reference counts to allocations would break the need to have a GC in the first place.
The best thing to do is to not use delete when uncertain about the uniqueness of a pointer.
More information about the Digitalmars-d
mailing list