Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 智能指针-程序终止时的分段错误_C++_Pointers_Stl_Unique_Shared - Fatal编程技术网

C++ 智能指针-程序终止时的分段错误

C++ 智能指针-程序终止时的分段错误,c++,pointers,stl,unique,shared,C++,Pointers,Stl,Unique,Shared,我一直在做一个状态的东西来替换我一直在使用的一些更糟糕的代码。我希望尽可能的现代化,所以我在适当的地方使用STL。我决定使用unique_ptr和shared_ptr,这是我以前从未使用过的。结果,我遇到了一个非常可怕的运行时错误:munmap_chunk():无效指针:0x00007fff7b3f08e0我只知道该错误与free()和malloc()有关,但我在代码中没有使用任何错误,所以我假设它来自STL内部。为了让事情变得更奇怪,如果我放置一个std::cout管理器(std::share

我一直在做一个状态的东西来替换我一直在使用的一些更糟糕的代码。我希望尽可能的现代化,所以我在适当的地方使用STL。我决定使用
unique_ptr
shared_ptr
,这是我以前从未使用过的。结果,我遇到了一个非常可怕的运行时错误:
munmap_chunk():无效指针:0x00007fff7b3f08e0
我只知道该错误与
free()
malloc()
有关,但我在代码中没有使用任何错误,所以我假设它来自STL内部。为了让事情变得更奇怪,如果我放置一个
std::cout管理器(std::shared_ptr(this));
}
无效状态管理器::pop(){
states.pop();
}
无效状态管理器::更改(元素和更改为(&U){
states.pop();
推送(标准::向前(更改为));
}
main.cpp:

#include "state_manager.hpp"
#include "state.hpp"
#include <iostream>

int main(int argc, const char* argv[]){
 state_manager test;
 test.push(std::make_unique<state>());
 test.change(std::make_unique<state>());
 test.pop();
}
#包括“state_manager.hpp”
#包括“state.hpp”
#包括
int main(int argc,const char*argv[]{
状态管理器测试;
test.push(std::make_unique());
test.change(std::make_unique());
test.pop();
}
在这一行代码中:

states.top()->manager(std::shared_ptr<state_manager>(this));
states.top()->管理器(std::shared_ptr(this));
您正在构建一个指向此的
共享\u ptr
。这意味着,当共享ptr的引用计数下降到零时,它将
删除
。这是不好的,因为该类不是此
的所有者。在您的情况下,
指针甚至不是由
new
分配的,因此共享ptr将无法删除它

您可以在<StesteMyMeals类中使用一个普通的C++指针,用于“代码> man <代码>,因为它目前不需要指针的所有权。

或者,您需要其他方法来获取指向此
共享
,例如,将其传递到
推送
(这会很奇怪)。

在这一行代码中:

states.top()->manager(std::shared_ptr<state_manager>(this));
states.top()->管理器(std::shared_ptr(this));
您正在构建一个指向此的
共享\u ptr
。这意味着,当共享ptr的引用计数下降到零时,它将
删除
。这是不好的,因为该类不是此
的所有者。在您的情况下,
指针甚至不是由
new
分配的,因此共享ptr将无法删除它

您可以在<StesteMyMeals类中使用一个普通的C++指针,用于“代码> man <代码>,因为它目前不需要指针的所有权。


或者,您需要其他方法来获取指向此
共享
,例如,将其传递到
推送
(这会很奇怪)。

您尝试过使用调试器吗?在Linux上,如果可以的话,应该会发生崩溃。不,我从未使用过调试器。是时候开始了?我推荐。如果在Linux上尝试,如果在Windows上尝试。学习此技能将比提供的任何解决方案更好。您是否尝试过使用调试器?在Linux上,如果可以的话,应该会发生崩溃。不,我从未使用过调试器。是时候开始了?我推荐。如果在Linux上尝试,如果在Windows上尝试。学习此技能将比提供的任何解决方案更好地为您服务。
#include "state_manager.hpp"
#include "state.hpp"

void state_manager::push(element&& to_push){
 states.push(std::forward<element>(to_push));
 states.top()->manager(std::shared_ptr<state_manager>(this));
}

void state_manager::pop(){
 states.pop();
}

void state_manager::change(element&& change_to){
 states.pop();
 push(std::forward<element>(change_to));
}
#include "state_manager.hpp"
#include "state.hpp"
#include <iostream>

int main(int argc, const char* argv[]){
 state_manager test;
 test.push(std::make_unique<state>());
 test.change(std::make_unique<state>());
 test.pop();
}
states.top()->manager(std::shared_ptr<state_manager>(this));