Search code examples
c++memory-managementboostshared-ptrweak-ptr

boost, shared ptr Vs weak ptr? Which to use when?


In my current project I am using boost::shared_ptr quite extensively.

Recently my fellow team mates have also started using weak_ptr. I don't know which one to use and when.

Apart from this, what should I do if I want to convert weak_ptr to shared_ptr. Does putting a lock on weak_ptr to create a shared_ptr affect my code in other thread?


Solution

  • In general and summary,

    Strong pointers guarantee their own validity. Use them, for example, when:

    • You own the object being pointed at; you create it and destroy it
    • You do not have defined behavior if the object doesn't exist
    • You need to enforce that the object exists.

    Weak pointers guarantee knowing their own validity. Use them, for example, when:

    • You access it, but it's not yours.
    • You have defined behavior if the object doesn't exist

    Lock() on a weak pointer returns a strong pointer; this is how you access the weak pointer. If the object is no longer valid (it's been deleted, etc), then the strong pointer will be NULL, otherwise, it will point at the object. You will need to check this.

    It's set up this way so that you cannot accidentally delete the object while you're using it, because you've made a temporary (local) strong pointer, and thus garunteed the object's existence while that strong pointer remains. When you're done using the object, you generally let the strong pointer fall out of scope (or reassigning it), which then allows the object to be deleted. For multithreading, treat them with same care you treat other things that don't have built-in thread safety, noting that the guarantee I mentioned above will hold when multithreading. AFAIK they don't do anything special past that.

    The boost shared pointers also have garbage-collector like features, since when the last strong pointer to an object goes away or points somewhere else, the object gets deleted.

    There's also the performance and circular dependencies mentioned in the other answers.

    Fundamentally, I would say that the boost shared pointer library allows you to not mess up putting together a program, but it is no substitute for taking the time to properly design your pointers, object ownerships and lifetimes. If you have such a design, you can use the library to enforce it. If you don't have such a design, you're likely to run into different problems than before.