weak pointers and use_count smart pointers

This is a discussion on weak pointers and use_count smart pointers within the C++ Programming forums, part of the General Programming Boards category; I've been reading about smart pointer comparison operators. http://www.open-std.org/jtc1/sc22/wg...004/n1590.html Namely, I was interested in understanding why the == operator is ...

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,473

    weak pointers and use_count smart pointers

    I've been reading about smart pointer comparison operators.
    http://www.open-std.org/jtc1/sc22/wg...004/n1590.html

    Namely, I was interested in understanding why the == operator is not defined for weak pointers. The link above taught me that not even for shared pointers is the == operator safe.

    A few doubts arised reading the above article. I would need your help understanding them:

    . What is meant by two shared pointers sharing ownership but pointing to different subobjects? How can this be?

    . what is meant by two shared pointers that have a custom deleter that doesn't delete? Is this a reference to bad code?

    . What is the address of weak pointers control block?

    Finally, not so related, I still have a little trouble understanding the advantage of weak pointers. I understand they don't increment the use count of the shared pointer they point to, however this comes at the expense of ending to manually manage my weak pointers when the shared pointer is deleted. Also, since they don't implement the == operator, i'm left with few choices at to the type of container I can use them in; sets and maps.
    Last edited by Mario F.; 07-29-2006 at 07:54 AM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    > What is meant by two shared pointers sharing ownership but pointing to different subobjects? How can this be?
    In meditating upon this question once you have the correct answer you realise why weak pointers exist. Shared pointers are smart because they keep their objects alive as long as its pointee has another reference around somewhere else. Say you have two pointers to an object; using raw pointers, the memory is freed as soon as one pointer is deleted. This leaves the other pointer dangling. This is the problem that shared pointer solves, as you know.

    Shared pointers become a problem once you develop complex relationships between objects, where say, Dad points to his daughter, and the daughter points to her father. This is two shared pointers sharing ownership but pointing to a different object... it's better known as a circular reference.
    Code:
    struct Dad {
        shared_ptr<Daughter> my_girl;
    };
    
    struct Daughter {
        shared_ptr<Dad> my_dad;
    };
    
    shared_pointer<Dad> parent( new shared_pointer<Dad> );
    shared_ptr<Daughter> child( new shared_ptr<Daughter> );
    
    parent->my_girl = child;
    child->my_dad = parent;
    
    child.reset( ); // leak here
    The two shared pointers you've created reference each other, therefore they will keep each other alive and are never properly disposed of. This is where weak pointers would be nice, because in contrast, if one object fell out of scope, the weak pointer deletes itself, but they can interact with other types of smart pointers.
    Code:
    struct Daughter {
       weak_ptr<Dad> my_dad;
    
       void play ( ) {
       shared_ptr<Dad> alive = my_dad.lock( ); 
       if( alive )
          // do something to parent
          // parent will be freed when it does go out of scope
       }
    };
    You almost notice that weak pointers are normal pointers, but they manage themselves. Since weak pointers can die at any time, if you compare two NULLed weak pointers with ==, code will be run when neither object exists.

    I've retyped part of an article. Perhaps other things will become clear if you read it. http://www.codeproject.com/vcpp/stl/boostsmartptr.asp

    > what is meant by two shared pointers that have a custom deleter that doesn't delete? Is this a reference to bad code?
    I think this applies to shared pointers to an object that overloads new and delete to use a different memory handling scheme (perhaps you append old objects to a static linked list or something, and operator new returns an old object if it exists instead of allocating another one).
    Last edited by whiteflags; 07-28-2006 at 11:23 PM.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,473
    Excellent. Thanks Citizen.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

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