What are potential dangers when using boost::shared_ptr?

后端 未结 13 748
猫巷女王i
猫巷女王i 2020-12-02 07:42

What are some ways you can shoot yourself in the foot when using boost::shared_ptr? In other words, what pitfalls do I have to avoid when I use boost::shared_ptr?

相关标签:
13条回答
  • 2020-12-02 08:20

    Giving out a shared_ptr< T > to this inside a class definition is also dangerous. Use enabled_shared_from_this instead.

    See the following post here

    0 讨论(0)
  • 2020-12-02 08:20

    Smart pointers are not for everything, and raw pointers cannot be eliminated

    Probably the worst danger is that since shared_ptr is a useful tool, people will start to put it every where. Since plain pointers can be misused, the same people will hunt raw pointers and try to replace them with strings, containers or smart pointers even when it makes no sense. Legitimate uses of raw pointers will become suspect. There will be a pointer police.

    This is not only probably the worst danger, it may be the only serious danger. All the worst abuses of shared_ptr will be the direct consequence of the idea that smart pointers are superior to raw pointer (whatever that means), and that putting smart pointers everywhere will make C++ programming "safer".

    Of course the mere fact that a smart pointer needs to be converted to a raw pointer to be used refutes this claim of the smart pointer cult, but the fact that the raw pointer access is "implicit" in operator*, operator-> (or explicit in get()), but not implicit in an implicit conversion, is enough to give the impression that this is not really a conversion, and that the raw pointer produced by this non-conversion is an harmless temporary.

    C++ cannot be made a "safe language", and no useful subset of C++ is "safe"

    Of course the pursuit of a safe subset ("safe" in the strict sense of "memory safe", as LISP, Haskell, Java...) of C++ is doomed to be endless and unsatisfying, as the safe subset of C++ is tiny and almost useless, as unsafe primitives are the rule rather than the exception. Strict memory safety in C++ would mean no pointers and only references with automatic storage class. But in a language where the programmer is trusted by definition, some people will insist on using some (in principle) idiot-proof "smart pointer", even where there is no other advantage over raw pointers that one specific way to screw the program state is avoided.

    0 讨论(0)
  • 2020-12-02 08:23

    Here are two things to avoid:

    • Calling the get() function to get the raw pointer and use it after the pointed-to object goes out of scope.

    • Passing a reference of or a raw pointer to a shared_ptr should be dangerous too, since it won't increment the internal count which helps keep the object alive.

    0 讨论(0)
  • 2020-12-02 08:29

    Creating multiple unrelated shared_ptr's to the same object:

    #include <stdio.h>
    #include "boost/shared_ptr.hpp"
    
    class foo
    {
    public:
        foo() { printf( "foo()\n"); }
    
        ~foo() { printf( "~foo()\n"); }
    };
    
    typedef boost::shared_ptr<foo> pFoo_t;
    
    void doSomething( pFoo_t p)
    {
        printf( "doing something...\n");
    }
    
    void doSomethingElse( pFoo_t p)
    {
        printf( "doing something else...\n");
    }
    
    int main() {
        foo* pFoo = new foo;
    
        doSomething( pFoo_t( pFoo));
        doSomethingElse( pFoo_t( pFoo));
    
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-02 08:30

    If you have a registry of the shared objects (a list of all active instances, for example), the objects will never be freed. Solution: as in the case of circular dependency structures (see Kaz Dragon's answer), use weak_ptr as appropriate.

    0 讨论(0)
  • 2020-12-02 08:31

    You need to be careful when you use shared_ptr in multithread code. It's then relatively easy to become into a case when couple of shared_ptrs, pointing to the same memory, is used by different threads.

    0 讨论(0)
提交回复
热议问题