傳說中的神器: shared_ptr/weak_ptr/scoped_ptr


ref page1 and page2

《Linux多線程服務端編程》稱 shared_ptr/weak_ptr 之為神器。

in multi-threading programming, we sometime get core dump when delete som obj ,even if we has do the check:  

if(obj)
   delete obj;

  However, core dump some out sometimes not always. My preview article has said something about this. And I avoid the problem by NOT delete them , using singleton pattern or ajust my logic.

      Yet, this artile is the right solution for multi-threading programming.

#include <string.h>
#include <iostream>
#include <boost/shared_ptr.hpp>

class implementation
{
    public:
        ~implementation(){std::cout<<"destroying implementation\n";}
        void do_something(){std::cout<<"did something\n";}
};

void test()
{
    boost::shared_ptr<implementation> sp1(new implementation);
    std::cout<< "The Sample now has "<< sp1.use_count()<< " reference(sp1)\n";

    //boost::shared_ptr<implementation> sp2(new implementation);
    boost::shared_ptr<implementation> sp2 = sp1;
    std::cout<< "The Sample now has "<< sp1.use_count()<< " reference(sp1)\n";
    std::cout<< "The Sample now has "<< sp2.use_count()<< " reference(sp2)\n";

    sp1.reset();
    std::cout<<"After Reset sp1. The Sample now has "<<sp1.use_count()<< " reference\n";
    std::cout<<"After Reset sp1. The Sample now has "<<sp2.use_count()<< " reference(sp2)\n";

    sp2.reset();//will call for ~implementation
    std::cout<<"After Reset sp2. The Sample now has "<<sp2.use_count()<< " reference\n";

}

int main(void)
{
    test();
    return 0;
}

=========================================================

For loop reference case , use weak_ptr

#include <string.h>
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>

class parent;
class children;

typedef boost::shared_ptr<parent> parent_ptr;
typedef boost::shared_ptr<children> children_ptr;

class parent
{
    public:
    ~parent(){std::cout<<"destroying parent\n";}
    public:
    boost::weak_ptr<children> child;    //changed from ori blog -- cause there was a complie error if use boost::weak_ptr<children> children
//    children_ptr children;
};

class children
{
    public:
    ~children(){std::cout<<"destroying children\n";}
    public:
    boost::weak_ptr<parent> par;//changed from ori blog for the same reason
//    parent_ptr parent;
};
void test()
{
    parent_ptr father(new parent());
    children_ptr son(new children());

    father->child = son;
    son->par = father;
}

int main(void)
{
    std::cout<<"begin test\n";
    test();
    std::cout<<"end test\n";
    return 0;
}

I change the code above ref to  the stackoverflow doc.

the difference between two of them:

強引用和弱引用

一個強引用當被引用的對象活着的話,這個引用也存在(就是說,當至少有一個強引用,那么這個對象就不能被釋放)。boost::share_ptr就是強引用。

相對而言,弱引用當引用的對象活着的時候不一定存在。僅僅是當它存在的時候的一個引用。弱引用並不修改該對象的引用計數,這意味這弱引用它並不對對象的內存進行管理,在功能上類似於普通指針,然而一個比較大的區別是,弱引用能檢測到所管理的對象是否已經被釋放,從而避免訪問非法內存。

 the define of 引用計數 may be can ref to some python blog

 總結一下對象會在一下情況下引用計數加1:

1.對象被創建:x=4

2.另外的別人被創建:y=x

3.被作為參數傳遞給函數:foo(x)

4.作為容器對象的一個元素:a=[1,x,'33']

引用計數減少情況

1.一個本地引用離開了它的作用域。比如上面的foo(x)函數結束時,x指向的對象引用減1。

2.對象的別名被顯式的銷毀:del x  ;或者del y

3.對象的一個別名被賦值給其他對象:x=789

4.對象從一個窗口對象中移除:myList.remove(x)

5.窗口對象本身被銷毀:del myList,或者窗口對象本身離開了作用域。

As for scoped_ptr:

#include <string.h>
#include <iostream>
#include <boost/scoped_ptr.hpp>

class implementation
{
    public:
        ~implementation(){std::cout<<"destroying implementation\n";}
        void do_something(){std::cout<<"did something\n";}
};

void test()
{
    boost::scoped_ptr<implementation> impl(new implementation());
    impl->do_something();
}

int main(void)
{
    std::cout<<"begin\n";
    test();
    std::cout<<"End\n";
    return 0;
}

It will automatically destroy when leave.

 

EOF


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM