2016-12-02 1 views
0

자유 시간에 작은 게임을하고 있는데, 코딩하는 동안 문제가 생겼습니다.이것은 일종의 포인터 오류입니까?

나는 문제를 분리하고 여기에 가능한 한 작게했습니다

#include <iostream> 
#include <memory> 
#include <vector> 
#include <Windows.h> 
class Base 
{ 
public: 
    std::string p; 
    virtual ~Base() 
    { 

    } 
    virtual std::vector<std::shared_ptr<std::string>> getText() = 0; 
}; 
class Derived1 : public Base 
{ 
public: 
    Derived1() 
    { 
     p = "from1"; 
    } 
    std::vector<std::shared_ptr<std::string>> getText() 
    { 
     return std::vector<std::shared_ptr<std::string>>{std::shared_ptr<std::string>(&p)}; 
    } 
}; 
class Derived2 : public Base 
{ 
public: 
    Derived2() 
    { 
     p = "from2"; 
    } 
    std::vector<std::shared_ptr<std::string>> getText() 
    { 
     return std::vector<std::shared_ptr<std::string>>{std::shared_ptr<std::string>(&p)}; 
    } 
}; 
int main() 
{ 
    std::unique_ptr<Base> state; 
    std::vector<std::shared_ptr<std::string>> displayText; 
    state.reset(new Derived1()); 
    displayText = state->getText(); 
    while(1) 
    { 
     for(auto i = displayText.begin(); i != displayText.end(); ++i) 
      std::cout << **i; 
     if (GetKeyState('2') < 0) 
     { 
      state.reset(new Derived2()); 
      displayText.clear(); 
      displayText = state->getText(); 
     } 
     else if (GetKeyState('1') < 0) 
     { 
      state.reset(new Derived1()); 
      displayText.clear(); 
      displayText = state->getText(); 
     } 
    } 
    return 0; 
} 

"1"을 눌러 "2"앞뒤가 가끔 작동하는 것 같다, 나는 I을 종료 할 때 -1073741510 또는 이와 유사한 값을 반환 값으로 가져옵니다. 내 게임 코드에서 그것은 또한 많이 충돌하는 것 같다.

난 아직도 이유를 이해할 수 없다

다형성 클래스와 함께 일반적으로 일부 unique_ptr에 대한 연구 및 포인터를 수행 한 후 (출구 밖으로까지 나는 명확히하지 않는, 죄송합니다 명령 창에서 X 버튼을 사용하여 의미) 그것이하는 것처럼 행동한다.

여기에 무슨 일이 발생합니까?

+0

"종료"를 정의하십시오. 표시된 코드는 프로그램을 완전히 종료 할 수있는 방법을 제공하지 않습니다. 그것은 무한 루프에서 실행됩니다. –

+0

@SamVarshavchik이 (가) 고정되어 있습니다. 명확하지 않은 점에 대해 사과드립니다. –

답변

0

스택 변수에서 shared_ptr을 만듭니다. 이렇게하면 안전하지 않은 p에서 delete가 호출됩니다. Calling delete on variable allocated on the stack

편집 : 포인터를 만들어야합니다. 다음과 같이 할 수 있습니다.

class Base 
{ 
public: 
    std::shared_ptr<std::string> p; 
    virtual ~Base() 
    { 

    } 
    virtual std::vector<std::shared_ptr<std::string>> getText() = 0; 
}; 
class Derived1 : public Base 
{ 
public: 
    Derived1() 
    { 
     p = std::make_shared<std::string>("from1"); 
    } 
    std::vector<std::shared_ptr<std::string>> getText() 
    { 
     return std::vector<std::shared_ptr<std::string>>{p}; 
    } 
}; 
class Derived2 : public Base 
{ 
public: 
    Derived2() 
    { 
     p = std::make_shared<std::string>("from2"); 
    } 
    std::vector<std::shared_ptr<std::string>> getText() 
    { 
     return std::vector<std::shared_ptr<std::string>>{p}; 
    } 
}; 
+0

좋아, 어쨌든이 문제를 해결할 수 있습니까? –

+0

포인터를 포인터로 만들어야합니다. 다음과 같이 할 수 있습니다 : 'class Base { public : \t std :: shared_ptr p; \t virtual ~ Base() {} \t 가상 std :: vector > getText() = 0; }; 클래스 Derived1 : 공공 자료 { 공개 : \t Derived1() \t { \t \t P = 표준 : make_shared ("from1"); \t} \t STD : 벡터 <표준 : shared_ptr의 > gettext에() { \t \t \t 복귀 STD : 벡터 <표준 : shared_ptr의 > {P}; \t} } ' –

+0

예, 방법이 있습니다. 'shared_ptr'의 문서를 보라. –