Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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++ - Fatal编程技术网

C++ 将方法转换为调用该方法的成员函子对象会导致崩溃

C++ 将方法转换为调用该方法的成员函子对象会导致崩溃,c++,C++,我在一个类中有以下方法: bool temp_cmp(size_t l, size_t r) const { return memcmp(get_next_temp_record(l), get_next_temp_record(r), page_size) < 0; } 原来整个类都被复制到了某个地方,然后函子对外部类的引用指向了旧位置==>内存损坏。看起来您有生命周期管理问题,当您访问唯一指针时,它似乎是空的。@但代码中的其他内容没有改变-所有具有唯一指针的向量保持不变-我只

我在一个类中有以下方法:

bool temp_cmp(size_t l, size_t r) const {
    return memcmp(get_next_temp_record(l), get_next_temp_record(r), page_size) < 0;
}

原来整个类都被复制到了某个地方,然后函子对外部类的引用指向了旧位置==>内存损坏。

看起来您有生命周期管理问题,当您访问唯一指针时,它似乎是空的。@但代码中的其他内容没有改变-所有具有唯一指针的向量保持不变-我只是从调用const方法切换到调用const
操作符()
成员函子对象的单个位置…
temp\u cmp\u函子
保留对排序器的引用-该排序器不会保持活动状态。如果创建函子时该发生了什么,则使用该函子的行为未定义。这就是我对你发布的代码所能说的。@Mat这都在
分类器
类本身中(以及初始化functor的
{*this}
,都在私有范围内)-程序中只创建了1个,并且调用了
temp\u cmp()
发生在
分拣机
类的另一个方法中…尝试创建一个再现问题的最小示例。问题中的信息是不够的。你所描述的不应该发生
struct temp_cmp_functor {
    const sorter& _s;
    temp_cmp_functor(sorter& s) : _s(s) {
        std::cout << "ADDR: " << &_s << std::endl;
    }

    bool operator()(size_t l, size_t r) const {
        std::cout << "ADDR AT CALL: " << &_s << std::endl;
        return memcmp(_s.get_next_temp_record(l), _s.get_next_temp_record(r), page_size) < 0;
    }
};
temp_cmp_functor temp_cmp{*this};
/usr/include/c++/9/bits/stl_vector.h:1061:34: runtime error: reference binding to null pointer of   type 'const struct value_type'
../../demos/ip_demo.cc:185:33: runtime error: member access within null pointer of type 'const struct   value_type'
AddressSanitizer:DEADLYSIGNAL
=================================================================
==3862==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000020 (pc 0x00000047a8b4 bp   0x7ffe9e0042c0 sp 0x7ffe9e0042a0 T0)
==3862==The signal is caused by a READ memory access.
==3862==Hint: address points to the zero page.
    #0 0x47a8b3 in std::__uniq_ptr_impl<char, seastar::free_deleter>::_M_ptr() const /usr/include/c++/  9/bits/unique_ptr.h:154
    #1 0x465ac5 in std::unique_ptr<char [], seastar::free_deleter>::get() const /usr/include/c++/9/ bits/unique_ptr.h:612
    #2 0x46635d in std::unique_ptr<char [], seastar::free_deleter>::operator[](unsigned long) const (/  home/onqtam/seastar/build/debug/demos/ip_demo+0x46635d)
    #3 0x45b146 in sorter::get_next_temp_record(unsigned long) const (/home/onqtam/seastar/build/   debug/demos/ip_demo+0x45b146)
    #4 0x45b3fe in sorter::temp_cmp_functor::operator()(unsigned long, unsigned long) const (/home/ onqtam/seastar/build/debug/demos/ip_demo+0x45b3fe)
    #5 0x420298 in operator() ../../demos/ip_demo.cc:421