Boost logo

Boost :

Subject: Re: [boost] deleting smart pointer (was [shared_ptr] delete shared_ptr)
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2011-03-18 14:02:13


AMDG

On 03/18/2011 10:08 AM, pavel wrote:
> Sid wrote on Friday, March 18, 2011 at 9:40:52:
>> I think it's simply stating an intention; the intention being that
>> the current shared pointer is no longer interesting in the shared
>> object; who cares if the shared object gets deleted under the
>> covers. This is the mentality that one needs to have in a
>> garbage-collected environment, since you get the same exact effect when you set a reference to null.
>> Having said that; I don't yet feel Pavel has convinced me that
>> changing the shared_pr library will buy anything significant.
> you got exactly my point!
>
> since for example 'smart_ptr' claims to be a pointer AND it allows
> such horrible thing like
>
> p = 0; //'p.reset()' is implied??

First of all, shared_ptr doesn't allow this. Second,
how is it horrible? Of course it is (almost) like
p.reset(). Assigning anything to a shared_ptr
decrements the reference count on the old value.

> why shouldn't it be 'delete'ed?

Because the behaviour of delete is
well-known. You want add a new
meaning which is similar enough to
cause confusion. I object to using
clever tricks like this just because
we can. In my opinion, the only
legitimate reason to "overload"
delete like this would be to allow
a template to treat raw pointers
and shared_ptrs polymorphically.
Such code is probably wrong anyway.

> the 'delete' operation does much less harm than that null assignment

That depends.

a) Using delete when you should use null assignment:
     probable memory corruption.
b) Using null assignment when you should use delete:
     resource leak.

Which is worse?

> otherwise it should be named like 'shared_guard' or something
>

Huh?

> and the semantics of deletion is well defined in the case of smart
> pointer,

Except that the tricks you use to implement
it result in undefined behavior...

> which states "i don't want the data pointed to anymore"
> (excuse me for repetition)

See, I think this is the basic problem.
Who is "I" here? Is it the local code or the
entire program? For the smart pointer case,
you're saying "the current function no longer
needs the object." For the raw pointer case,
you're saying "nothing in this program needs
the object any more." These are fundamentally
different, therefore IMHO, spelling them the same
way is only going to cause problems.

> eventually the user has done the duty 'delete'ing the data and
> generally doesn't care if the data gets deleted immediately or when
> all other references quit
>
> sorry for not replying every message, i hope this answers all those
> questions

In Christ,
Steven Watanabe


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