Thread: smart pointer release

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Elysia View Post
    If two threads use the same variable then yes.
    No it is not a problem!

    The fundamentals of COM require that incrementing and decrementing the reference count be threadsafe, assuming you've correctly uses CComMultiThreadModel like you're supposed to if your COM object can live in a multi-threaded apartment. In this case it then uses InterlockedDecrement, so only one thread can ever see the refcount be decremented to zero. Furthurmore it would be a bug if the refcount were ever one and multiple threads called Release, though that still would not cause more than one thread to try and delete the object.
    Thus only one of the threads can ever call delete on the object.

    As I said, I use COM every day at work, and I don't think Elysia has used COM much, if at all.
    You can certainly trust Elysia for a quick answer, but often not for a correct one.
    Last edited by iMalc; 02-11-2008 at 02:35 AM.
    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"

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by iMalc View Post
    No it is not a problem!
    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.

    As I said, I use COM every day at work, and I don't think Elysia has used COM much, if at all.
    You can certainly trust Elysia for a quick answer, but often not for a correct one.
    No always correct, no, but no one can be.
    I have used COM somewhat, but never really implemented it nor used it in a threaded environment, so that is beyond me.
    I don't know the depths of COM, either. I know how to use it, AddRef, Release, QueryInterface, CoCreateInstance, etc. And frankly, COM annoys me, so I avoid it. My knowledge on COM itself isn't perhaps what it should be.
    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. #3
    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. #4
    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. #5
    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. #6
    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. #7
    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"

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