我在一个类中有以下方法:
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;
}但我需要它作为一个函数对象,所以我重写了它,如下所示:
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};现在,后来当我像temp_cmp(idx1, idx2)一样调用它时,我在使用ASAN (函数get_next_temp_record访问类的一些类型为std::vector的字段,但我认为这不应该是相关的)的清理构建中崩溃。
你知道为什么这个改变可能会有问题吗?我正在打印对象的地址,它们在函数器的构造函数中匹配,当operator()被调用时-我认为这没有理由不起作用……对我来说,这两种方式应该是相同的…我遗漏了什么?
以下是ASAN的输出:
/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发布于 2020-02-17 21:55:08
结果是整个类被复制到了某个地方,然后函数器对外部类的引用指向了旧的位置==>内存损坏。
https://stackoverflow.com/questions/60247203
复制相似问题