できる限りSSCEを作成しました。私の疑いは、メインでオブジェクトを要求する前に、共有ポインターがオブジェクトを分解 (解放) することです。共有ポインタを完全に回避せずにこれを防ぐにはどうすればよいですか? これは、shared_ptrs の使用によって大いに助けられるプログラムの孤立した問題です。
Test.h:
#ifndef TEST_H
#define TEST_H
#include <memory>
#include <iostream>
#include <vector>
class Test
{
public:
Test();
virtual ~Test();
static std::shared_ptr<Test> makeTestFrom(std::string l);
std::vector<std::shared_ptr<int>> reg_vec;
protected:
private:
};
#endif // TEST_H
テスト.cpp
#include <memory>
#include <iostream>
#include <vector>
Test::Test():
reg_vec()
{
//ctor
}
Test::~Test()
{
//dtor
}
std::shared_ptr<Test> Test::makeTestFrom(std::string l)
{
std::shared_ptr<Test> sp(new Test());
std::shared_ptr<int> i(new int(3));
sp->reg_vec.push_back(i);
return sp;
}
main.cpp:
#include <memory>
#include <iostream>
#include <vector>
#include "include/Test.h"
using namespace std;
int main()
{
std::unique_ptr<Test> x(new Test());
x->makeTestFrom("loldoesntmatter");
std::cout << x->reg_vec[0] << std::endl;
return 0;
}