unique_ptr在指向的对象上调用operator[]()时出现分割错误
unique_ptr Segmentation fault when calling operator[]() on pointed to object
我正在努力把我的头围绕一个讨厌的运行时错误。这是一个有点代码发布,但我希望它不是太多的阅读和太少的诊断问题。由于某种原因,当我遵从它并使用操作符函数调用时,unique_ptr会爆炸。
class Dataset
{
private:
std::unique_ptr<File> file;
std::unique_ptr<File> & getFile() throw () { return file; }
std::unique_ptr<Properties> properties;
std::unique_ptr<Properties> & getProperties() throw ()
{
return properties;
}
...
}
class Properties
{
private:
std::map<const std::string, Property> container;
public:
Property & operator[](const std::string & s) {
try
{
return container.at(s);
}
catch (std::out_of_range & e)
{
std::stringstream ss;
ss << "Key "" << s << "" does not exist in collection in
file " << __FILE__ << " at line " << __LINE__;
throw Exception::KeyNotFound(ss.str(), __FILE__, __LINE__);
}
}
...
}
class FrameCollection
{
private:
Dataset & dataset;
public:
FrameCollection();
...
}
FrameCollection::FrameCollection()
{
Property property (
dataset.getProperties()->operator[](PROPERTY_MAX_FRAMES)
);
...
}
唯一指针在FrameCollection()中爆炸:
线程[1]14303 [core: 10] (suspend: Signal: SIGSEGV:Segmentation fault)std::_Rb_tree, std::_Select1st>, std::less, std::allocator>>::_M_begin() at stl_tree.h:502 0x7ffff7ba9d72
std::_Rb_tree, std::_Select1st>, std::less, std::allocator>>::lower_bound() at stl_tree.h:879 0x7ffff7bbbd4e
Std::map, Std::allocator>>::lower_bound() at stl_map.h:864 0x7ffff7bbba39
Std::map, Std::allocator>>::at() at stl_map.h:503 0x7ffff7bbb762bmd2::PropertyCollection::operator at persistenobject .cpp:140 0x7ffff7bb9137
bmd2::FrameCollection::FrameCollection() at FrameCollection.cpp:16 0x7ffff7bb5425
bmd2::Dataset::Dataset() at Dataset.cpp:68 0x7ffff7ba61f9
__gnu_cxx::new_allocator::construct>() at new_allocator.h:120 0x7ffff7ba3b67
std::allocator_traits>::_S_construct>() at alloc_traits.h:254 0x7ffff7ba3977
Std::allocator_traits>::construct>() at alloc_traits.h:393 0x7ffff7ba37b7
& lt;…多帧…>
问题是在你的FrameCollection
类。你定义了一个引用,但没有在构造函数中初始化它。
class FrameCollection
{
private:
Dataset & dataset;
public:
FrameCollection();
// ...
}
FrameCollection::FrameCollection()
{
Property property (
dataset.getProperties()->operator[](PROPERTY_MAX_FRAMES)
);
//...
}
当类中有引用变量时,构造函数应该接受引用并在初始化列表中初始化该成员:
class FrameCollection
{
private:
Dataset & dataset;
public:
FrameCollection( Dataset& );
// ...
}
FrameCollection::FrameCollection( Dataset& d ) :
dataset( d )
{
Property property (
dataset.getProperties()->operator[](PROPERTY_MAX_FRAMES)
);
//...
}
- C++映射分割错误(核心转储)
- 由cin中的字符串中未捕获空白引起的分割错误
- 删除映射和分割错误中的一个过去结束元素
- 在指向函数中读取变量时出现分割错误
- 在链表中的第 n 位插入显示分割错误
- 较高值 n 的分割错误(例如 n=999997)
- 尝试通过memcpy复制大尺寸浮点向量时的分割错误
- 分割错误:向量中的擦除功能
- 向量向量的分割错误
- 我在C++中编写了一个方法来打印树类的预序,但它显示了分割错误
- C ++分割错误,为什么使用"long long"我没有得到答案?
- 在尝试使用递归查找集合子集的总数时,我遇到了分割错误
- 分割错误 11:尝试使用 cin 输入 B[1] 时
- 集合布局上的 Qt 分割错误
- 高达20亿的筛子会产生分割错误
- G :内部编译器错误:分割故障(程序CC1PLUS) - 我在哪里开始
- 二进制搜索树操作程序错误:分割故障(核心倾倒)
- 在给出正确的输出后,给出错误分割错误(核心转储)
- 得到错误分割错误(核心转储)进程返回139 (0x8B)
- 内部编译错误:分割错误在gcc.发送可变模板到struct时