Re: VS2012 VC++ std::unique_ptr broken!

Discussion in 'C++' started by Martin Ba, Nov 22, 2012.

  1. Martin Ba

    Martin Ba Guest

    On 22.11.2012 00:39, Leigh Johnston wrote:
    > Hi,
    >
    > From
    > https://connect.microsoft.com/Visua...ct-before-resetting-pointer-rather-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, Nov 22, 2012
    #1
    1. Advertising

  2. Martin Ba

    Martin Ba Guest

    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
     
    Martin Ba, Nov 23, 2012
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Micha³ 'Khorne' Rzechonek

    C++0x: unique_ptr and std::move

    Micha³ 'Khorne' Rzechonek, Jan 28, 2009, in forum: C++
    Replies:
    7
    Views:
    6,990
  2. Jayden Shui

    Is this a bug of std::unique_ptr?

    Jayden Shui, Dec 16, 2011, in forum: C++
    Replies:
    4
    Views:
    875
  3. Jayden Shui
    Replies:
    2
    Views:
    375
    Jayden Shui
    Dec 16, 2011
  4. Brice Gagnage
    Replies:
    6
    Views:
    1,671
    Brice Gagnage
    Apr 5, 2012
  5. Replies:
    0
    Views:
    186
Loading...

Share This Page