boost  shared_ptr 은 편한 놈이지만..

문제가 생기는 상황이 몇 가지 있다 있다그 중 하나 바로 객체 내부에서 전역 함수(혹은 다른 class 의 함수)를 호출 할 때 this 를 넘기는 문제인데

 

왜나면 아래와 같은 코드가 안되기 때문이다.

 

shared_ptr과 포인터형은 서로 대입이 되지 않기 때문에 위와 같은 코드는 컴파일 에러를 뱉는다그럼 아래와 같이 고치면 어떨까.

 

 

this 를 shared_ptr<SPtrTestthisptr(this);

로 변환 해서 넘겻다.

 

이렇게 하면.

SPtrTest::Operation이 끝날때와 Func 가 끝날 때 2 destructor 가 호출 되게 되어 에러가 난다.

이걸 방지하는 방법은 자기 자신의 shared_ptr 을 레퍼런스 카운트 문제가 생기지 않도록(this 를 넘길 때도 레퍼런스 카운터가 증가 하는하는 것이다.

 

즉 boost::enable_shared_from_this<T> 를 상속 받은 class 를 만드는 방법이다.

boost::enable_shared_from_this<T> 를 상속 받은 뒤 shared_from_this() 를 호출 하면 자기 자신의 shared_ptr 을 리턴 해 준다.

 

그럼 아래와 같이 된다

#include <boost/shared_ptr.hpp>

#include <cstdio>

 

using namespace boost;

 

class SPtrTest;

 

void Func(shared_ptr<SPtrTestsp);

 

class SPtrTest

{

public:

        int var1;

        SPtrTest()

        {

               printf("Constructor\n");

        }

        ~SPtrTest()

        {

               printf("Destructor\n");

        }

        void operation()

        {

              

               Func(this);

        }

};

 

void Func(shared_ptr<SPtrTestsp)

{

        sp->var1++;

        printf("%d\n"sp->var1);

};

 

int _tmain(int argc_TCHARargv[])

{

 

        shared_ptr<SPtrTesttestclass(new SPtrTest);

        testclass->var1 = 0;

        testclass->operation();

              

        return 0;

}

 

이렇게 하면 아무 문제 없이 작동이 가능하다.

 

 

 

Ps. 특히 조심해야 할 것은..

shared_ptr<SPtrTestthisptr(this); 를 여러 번 생성 한다고 레퍼런스 카운터가 증가하는 것은 아니다레퍼런스 카운터의 증가는 변수끼리의 대입에 의해서만 증가할 뿐저런 선언은 카운터1짜리 변수를 여러 개 늘려 에러를 내게 할 뿐이다.

#include <boost/shared_ptr.hpp>

#include <cstdio>

 

using namespace boost;

 

class SPtrTest;

 

void Func(shared_ptr<SPtrTestsp);

 

class SPtrTest

{

public:

        int var1;

        SPtrTest()

        {

               printf("Constructor\n");

        }

        ~SPtrTest()

        {

               printf("Destructor\n");

        }

        void operation()

        {

               shared_ptr<SPtrTestthisptr(this);

               Func(thisptr);

        }

};

 

void Func(shared_ptr<SPtrTestsp)

{

        sp->var1++;

        printf("%d\n"sp->var1);

};

 

int _tmain(int argc_TCHARargv[])

{

 

        shared_ptr<SPtrTesttestclass(new SPtrTest);

        testclass->var1 = 0;

        testclass->operation();

              

        return 0;

}




#include <boost/shared_ptr.hpp>

#include <boost/weak_ptr.hpp>

#include <boost/smart_ptr/enable_shared_from_this.hpp>

#include <cstdio>

 

using namespace boost;

 

class SPtrTest;

 

void Func(shared_ptr<SPtrTestsp);

 

class SPtrTest : public boost::enable_shared_from_this<SPtrTest>

{

public:

        int var1;

        SPtrTest()

        {

               printf("Constructor\n");

        }

        ~SPtrTest()

        {

               printf("Destructor\n");

        }

        void operation()

        {

               Func(shared_from_this());

        }

};

 

void Func(shared_ptr<SPtrTestsp)

{

        sp->var1++;

        printf("%d\n"sp->var1);

};

 

int _tmain(int argc_TCHARargv[])

{

 

        shared_ptr<SPtrTesttestclass(new SPtrTest);

        testclass->var1 = 0;

        testclass->operation();

              

        return 0;

}

조회 수 :
13349
등록일 :
2010.03.01
15:02:12 (*.186.67.74)
엮인글 :
http://www.lunapiece.net/4187/201/trackback
게시글 주소 :
http://www.lunapiece.net/4187
문서 첨부 제한 : 0Byte/ 10.00MB
파일 제한 크기 : 10.00MB (허용한 확장자만 첨부할 수 있습니다.(관리자는 제외) : *.*)
옵션 :
:
:
:
:
List of Articles
번호 제목 글쓴이 날짜 조회 수
33 C++11 lambda 간단사용법과 성능비교. file Lyn 2012-01-29 120
32 Boost 빌드 방법 [1] Lyn 2012-01-13 401
31 DLLMain에서 하지 말아야 할것. Lyn 2012-01-12 540
30 새해다 ... Lyn 2011-12-31 539
29 Trac, Mantis, Redmine + SVN 소스 한글 깨짐 Lyn 2011-12-20 867
28 MultiMedia Timer 의 Timer 해상도의 비밀 file Lyn 2011-12-14 944
27 Intel Cilk plus And SampleCode - Parallel Programming file Lyn 2011-11-22 1195
26 Visaul Studio Exception 발생시 해결방법 file Lyn 2011-11-09 2008
25 [Boost 살펴보기] 9. noncopyable Lyn 2011-09-14 3242
24 Mysql 툴 Lyn 2011-03-30 9784
23 [개인자료] 프로그램 만들때 무조건 해야하는것.. Lyn 2011-01-25 11542
22 사용중인 FireFox 플러그인. Lyn 2010-05-18 12840
21 Google C++ Coding Style - 번역중 Lyn 2010-03-28 14090
» Boost 에서 자기 자신의 shared_ptr 리턴하기 file Lyn 2010-03-01 13349
19 [잡설] 앱스토어의 유행. 프로그래머에겐 과연? Lyn 2010-02-19 13328
18 [잡설]델파이 / C++ Builder 하는 사람들의 문제점. Lyn 2010-02-06 13953
17 [개인자료] 윈도우 재설치 후 설치 하는 프로그램 Lyn 2010-01-03 13512
16 Compare, Merge 툴 간의 비교. - 작성중 [1] Lyn 2009-12-07 14454
15 C++ new 연산자의 진실 file Lyn 2009-08-18 16183
14 [Boost 살펴보기] 8. Tokenizer file Lyn 2009-06-10 15549

SITE LOGIN :)