------- Comment #10 from tjgolubi at netins dot net 2010-03-01 22:22 -------
Subject: Re: std::unique_ptr::reset() does not conform to N3035.
I see your point.
I think it should still check for resetting to the same value to avoid
duplicate deletes later.
terry
void
reset(pointer __p = pointer())
{
pointer old_p = get();
if (__p != old_p)
{
std::get<0>(_M_t) = __p;
if (old_p != pointer()) // <-------- added this line
get_deleter()(old_p);
}
}
----- Original Message -----
From: "jwakely dot gcc at gmail dot com" <[email protected]>
To: <[email protected]>
Sent: Monday, March 01, 2010 9:05 AM
Subject: [Bug libstdc++/43183] std::unique_ptr::reset() does not conform to
N3035.
>
>
> ------- Comment #5 from jwakely dot gcc at gmail dot com 2010-03-01
> 15:05 -------
> OK, I'm back and have had time to look at this. I vaguely remember
> noticing
> that the assignment and the deleter invocation happened in the wrong order
> in
> our implementation, but I must have forgotten about it as I don't have any
> uncommitted change (or even a comment) in my working copy.
>
> The suggested change is still incorrect: the wrong condition is checked.
> The
> deleter must be invoked when old_p != NULL, rather than when old_p != p.
> Consider:
>
> unique_ptr<int> p1;
> p1.reset(new int);
>
> The deleter should not be invoked by the call to reset, because old_p ==
> nullptr.
>
> Another case:
>
> unique_ptr<int> p(new int);
> p.reset(p.get());
> p.release();
>
> This should not leak, but with the suggested change it will, because the
> deleter will not get invoked.
>
> A better implementation would be (untested):
>
> void
> reset(pointer __p = pointer())
> {
> pointer __old = get();
> std::get<0>(_M_t) = __p;
> if (__old != 0)
> std::get<1>(_M_t)(__old);
> }
>
>
> --
>
>
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43183
>
> ------- You are receiving this mail because: -------
> You reported the bug, or are watching the reporter.
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43183