Thread: smart pointer release

  1. #16
    Banned
    Join Date
    Nov 2007
    Posts
    678
    approach 1 is better (iiuc), if there is a smart pointer involved.
    because safe pointers release memory when the referent goes out of scope.
    the tmp var created inside func scope will cause the smart pointer to trigger a mem free.

  2. #17
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Actually I think I've figured it out. It's to do with re-entrancy.
    Ah, yes, the re-entrancy problem. Makes sense.

    However, it's still misguided. Namely: this happens if the refcount drops to zero. But actually, once it drops to zero, no one is supposed to hold a pointer on which they can call Release anymore.
    What can happen is that the destructor passes itself elsewhere, but that would be a really bad idea: the elsewhere place is allowed to call AddRef and keep the object - but it's already in the unstoppable process of being destroyed!

    Edit: Wait, I just re-read your post. Yes, that does make sense.
    Last edited by CornedBee; 02-11-2008 at 06:01 AM.
    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

  3. #18
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Elysia View Post
    Of course it's a problem. It all depends on the implementation of the smart pointer class. Mayhap COM itself may be thread safe, but no the class itself.
    In th COM case, the functions that deal with the Refcounting are part of the class itself (intrusive), not part of the smart pointer class. Whether you use CComPtr<IUnknown> or IUnknownPtr, or some other home made smart pointer, they all have to go through to calls to Release on the IUnknown interface itself.
    You're probably thinking of boost's shared_ptr with its non-intrusive refcounting.

    Now that in itself doesn't automatically mean that a home grown start pointer class is instantly perfect, for example it is easy to get the assignment operator wrong. But it does still mean that the destructor will only ever get called once, purely because it is COM that is responsible for this and not the smart pointer itself, which is only responsible for calling AddRef and Release at the right time etc.
    People should have no need to use homegrown smartpointers with COM anyway.
    Last edited by iMalc; 02-11-2008 at 12:18 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #19
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks iMalc,


    Could you help to explain what means non-intrusive refcounting and intrusive refcounting please?

    Quote Originally Posted by iMalc View Post
    In th COM case, the functions that deal with the Refcounting are part of the class itself (intrusive), not part of the smart pointer class. Whether you use CComPtr<IUnknown> or IUnknownPtr, or some other home made smart pointer, they all have to go through to calls to Release on the IUnknown interface itself.
    You're probably thinking of boost's shared_ptr with its non-intrusive refcounting.

    Now that in itself doesn't automatically mean that a home grown start pointer class is instantly perfect, for example it is easy to get the assignment operator wrong. But it does still mean that the destructor will only ever get called once, purely because it is COM that is responsible for this and not the smart pointer itself, which is only responsible for calling AddRef and Release at the right time etc.
    People should have no need to use homegrown smartpointers with COM anyway.
    Thanks manav,


    I do not understand this benefits you mentioned, "the tmp var created inside func scope will cause the smart pointer to trigger a mem free". Could you show some pseudo code or more description please?

    Quote Originally Posted by manav View Post
    approach 1 is better (iiuc), if there is a smart pointer involved.
    because safe pointers release memory when the referent goes out of scope.
    the tmp var created inside func scope will cause the smart pointer to trigger a mem free.

    regards,
    George

  5. #20
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Intrusive refcounting would be the objects that your smart pointer class is meant to hold actually each contain the variable that holds their own reference count. This is often done by deriving from a base class that contains the reference count member. The smart pointer class then only needs to hold a pointer to the item, and can manipulate the reference count through that.

    Non-intrusive would be where a seperate pointer to the reference count (which is allocated by the smart pointer class) is stored in the smart pointers that point to the same. This means the smart pointer has two pointer members, on of which is to memory allocated by it. But the class it is meant to hold doesn't have anything besides its own data, and doesn't ever know that it is being held in a smart pointer, or that its days are numbered by some refcount out there.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #21
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks iMalc,


    In Non-intrusive mode, the component itself does not need to manage refcount itself? And only the smart pointer wrapper class manages the refcount?

    Quote Originally Posted by iMalc View Post
    Intrusive refcounting would be the objects that your smart pointer class is meant to hold actually each contain the variable that holds their own reference count. This is often done by deriving from a base class that contains the reference count member. The smart pointer class then only needs to hold a pointer to the item, and can manipulate the reference count through that.

    Non-intrusive would be where a seperate pointer to the reference count (which is allocated by the smart pointer class) is stored in the smart pointers that point to the same. This means the smart pointer has two pointer members, on of which is to memory allocated by it. But the class it is meant to hold doesn't have anything besides its own data, and doesn't ever know that it is being held in a smart pointer, or that its days are numbered by some refcount out there.

    regards,
    George

  7. #22
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Yes, exactly.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #23
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks iMalc,


    I have read all of your posts in this thread again. Seems your point is (or preferred approach), smart pointer should delegate ref counter function to wrapper component (intrusive mode), right?

    If yes, please check my original post of the question, I think my original code is also intrusive mode, which delegating ref counter to the wrapper component.

    Any comments?

    Quote Originally Posted by iMalc View Post
    Yes, exactly.

    regards,
    George

  9. #24
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Seems your point is (or preferred approach), smart pointer should delegate ref counter function to wrapper component (intrusive mode), right?
    No, that's just the way it works in COM. Both approaches have their advantages and disadvantages. Intrusive refcounting has less overhead. Non-intrusive works without having to modify the target type.
    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

  10. #25
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks for sharing your advice, CornedBee!


    Quote Originally Posted by CornedBee View Post
    No, that's just the way it works in COM. Both approaches have their advantages and disadvantages. Intrusive refcounting has less overhead. Non-intrusive works without having to modify the target type.

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. scope of a pointer?
    By Syneris in forum C++ Programming
    Replies: 6
    Last Post: 12-29-2005, 09:40 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM