Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Re: VS2012 VC++ std::unique_ptr broken! (http://www.velocityreviews.com/forums/t954762-re-vs2012-vc-std-unique_ptr-broken.html)

Martin Ba 11-22-2012 07:02 PM

Re: VS2012 VC++ std::unique_ptr broken!
 
On 22.11.2012 00:39, Leigh Johnston wrote:
> Hi,
>
> From
> https://connect.microsoft.com/Visual...her-than-after
> :
>
> "ISO C++11 mandates that std::unique_ptr<T>::reset() deletes the owned
> object *after* resetting the pointer to the owned object rather than
> before as VC10 and VC11 are currently doing.
>
> ...
> This can cause a problem if the destructor of the owned object calls
> back to code which attempts to call reset() a second time on the same
> unique_ptr object resulting in double delete; workaround is to take a
> local copy of the unique_ptr object rather than use reset()."
>


Not sure I get the workaround. Can you post a code sample?

cheers,
Martin


Martin Ba 11-23-2012 09:33 AM

Re: VS2012 VC++ std::unique_ptr broken!
 
On 22.11.2012 22:07, Leigh Johnston wrote:
> On 22/11/2012 21:06, Leigh Johnston wrote:
>> class wibble
>> {
>> typedef std::unique_ptr<foo> foo_pointer;
>> foo_pointer m_MyFoo;
>> ...
>> ...

> Oops; that should have been:
>
> class wibble
> {
> typedef std::unique_ptr<foo> foo_pointer;
> foo_pointer m_MyFoo;
> ...
> ...
> void f()
> {
> ...
> ...
> { foo_pointer destroyingCopy(m_MyFoo); // instead of
> m_MyFoo.reset(); }
> ...
> ...
> }
> ...
> ...
> };
>


I'm confused. I thought you cannot copy a unique_ptr -- should that be

foo_pointer destroyingCopy(std::move(m_MyFoo));

instead?

cheers,
Martin


All times are GMT. The time now is 01:16 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57