Boost logo

Boost :

Subject: [boost] deleting smart pointer
From: pavel (paul.cpprules_at_[hidden])
Date: 2011-03-18 16:54:48


 Steven wrote on Friday, March 18, 2011 at 21:02:13:
> First of all, shared_ptr doesn't allow this.

agree, my bad

> 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.

rephrasing your words: how is 'delete p' a bad idea? it's obvious it
is almost like 'p.reset()'

deleting a smart pointer decrements the reference counter and releases
the memory if the count drops to zero

> 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.

that's what we try to figure out here

and no, it's the other way around: the unfairness that smart pointers
can not be deleted like plain pointers led to the development of the
fancy stuff

> 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?

both

the intention when deleting a smart pointer is to gently release the
reference to the data it currently points to -- no memory corruption
or leak intended

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

i inspected the std and didn't find anything which may result in
undefined behavior

did i miss anything?

> 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.

i'll rephrase it: deleting a pointer states that the scope of the
pointer instance does not need the data pointed to (by the pointer
instance)

here a pointer may be a plain pointer or a smart one; the scope of the
pointer may be a function or whole program

no offence but is my english really so bad that almost noone can
understand what i am talking about?

-- 
Pavel
P.S.
if you notice a grammar mistake or weird phrasing in my message
please point it out

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