Is shared_ptr dangerous?

This is a discussion on Is shared_ptr dangerous? within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by CornedBee The commonly accepted definition of detach() is that it is a function that makes the smart ...

  1. #16
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by CornedBee View Post
    The commonly accepted definition of detach() is that it is a function that makes the smart pointer stop managing the memory it points to. In other words, it means, "give me the raw pointer and set yourself to NULL".
    In std::auto_ptr, this function is called release().

    If the above is not what Elysia meant by Detach(), we've wasted a lot of typing ...
    OK thanks, I'm familiar with release(), but I never saw a detach() before.
    I can certainly see the danger of having a release() or detach() function in a shared_ptr.

  2. #17
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,624
    Quote Originally Posted by CornedBee View Post
    The commonly accepted definition of detach() is that it is a function that makes the smart pointer stop managing the memory it points to. In other words, it means, "give me the raw pointer and set yourself to NULL".
    In std::auto_ptr, this function is called release().

    If the above is not what Elysia meant by Detach(), we've wasted a lot of typing ...
    Yes, that's what it means. I wanted to release the smart pointer its duty to manage to memory and give it to be under a certain circumstance. ATL's CComPtr, for example, supports this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,261
    O_o

    Does 'CComPtr:etach' alter the reference? Or does it just return the pointer and null the member for that particular instance of 'CComPtr'?

    Can you give more information about this "certain circumstance"?

    Soma

  4. #19
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,624
    Releases ownership of a pointer, sets the CComPtrBase::p data member variable to NULL, and returns a copy of the pointer.
    That's why CComPtr::Detach does. I can't remember more details right now. It's just from the documentation.
    But it should guarantee that it returns the pointer and does not Release it beforehand (which shared_ptr would do).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    CComPtr has the advantage that the reference counting of COM objects is internal and publicly documented. If a shared_ptr gives up its pointer, it has to decrement the refcount, because there's no way for the user to do it.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why is duplicate code dangerous?
    By agentsmith in forum C Programming
    Replies: 14
    Last Post: 01-08-2008, 12:10 AM
  2. Is sem_getvalue() dangerous?
    By Mr_Miguel in forum C Programming
    Replies: 3
    Last Post: 01-01-2008, 12:54 PM
  3. Dangerous System Functions for Servers ???
    By Moni in forum C++ Programming
    Replies: 6
    Last Post: 09-30-2003, 10:55 PM
  4. Why is the gets function dangerous?
    By Kevin.j in forum C Programming
    Replies: 2
    Last Post: 09-27-2002, 05:18 PM
  5. the gets() function is dangerous
    By itld in forum Linux Programming
    Replies: 8
    Last Post: 12-27-2001, 06:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21