目前,我正在尝试为我们需要为大学构建的数据库系统实现某种基本的文件访问层。为此,我被要求在内存中缓存页面。为了快速确定尚未加载哪些页面,我将所有页面存储在unordered_map中。
现在的问题是,在对该函数进行一些调用之后,SIGSEGV出现在unordered_map的_M_find_before_node内。我尝试使用gdb调试它,但是不知道在哪里寻找它,因为在调用unordered_map时发生错误:
#0 0x0000000000402ab0 in std::_Hashtable<unsigned long, std::pair<unsigned long const, BufferFrame>, std::allocator<std::pair<unsigned long const, BufferFrame> >, std::__detail::_Select1st, std::equal_to<unsigned long>, std::hash<unsigned long>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<false, false, true> >::_M_find_before_node (this=0x608038, __n=2, __k=@0x7fffffffe050: 2, __code=2) at /usr/include/c++/4.9/bits/hashtable.h:1442
#1 0x0000000000402286 in std::_Hashtable<unsigned long, std::pair<unsigned long const, BufferFrame>, std::allocator<std::pair<unsigned long const, BufferFrame> >, std::__detail::_Select1st, std::equal_to<unsigned long>, std::hash<unsigned long>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<false, false, true> >::_M_find_node (this=0x608038, __bkt=2, __key=@0x7fffffffe050: 2, __c=2) at /usr/include/c++/4.9/bits/hashtable.h:625
#2 0x0000000000401cd6 in std::_Hashtable<unsigned long, std::pair<unsigned long const, BufferFrame>, std::allocator<std::pair<unsigned long const, BufferFrame> >, std::__detail::_Select1st, std::equal_to<unsigned long>, std::hash<unsigned long>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<false, false, true> >::find (this=0x608038, __k=@0x7fffffffe050: 2) at /usr/include/c++/4.9/bits/hashtable.h:1304
#3 0x000000000040198f in std::unordered_map<unsigned long, BufferFrame, std::hash<unsigned long>, std::equal_to<unsigned long>, std::allocator<std::pair<unsigned long const, BufferFrame> > >::find (
this=0x608038, __x=@0x7fffffffe050: 2) at /usr/include/c++/4.9/bits/unordered_map.h:574
#4 0x00000000004013dd in BufferManager::fixPage (this=0x608030, pageId=2, exclusive=true) at BufferManager.cpp:36
#5 0x0000000000404268 in main (argc=4, argv=0x7fffffffe2c8) at buffertest.cpp:87
我还尝试使用valgrind对其进行调试,因为我想到在出现错误之前的某个时间点我可能会注意到一个未引起注意的内存损坏。这给了我以下输出:
==27031== Memcheck, a memory error detector
==27031== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==27031== Using Valgrind-3.10.0 and LibVEX; rerun with -h for copyright info
==27031== Command: ./buffer-test 11 3 1
==27031==
==27031== Invalid write of size 8
==27031== at 0x401642: std::__detail::_Hash_node_base::_Hash_node_base() (hashtable_policy.h:234)
==27031== by 0x4020A0: std::_Hashtable<std::string, std::pair<std::string const, int>, std::allocator<std::pair<std::string const, int> >, std::__detail::_Select1st, std::equal_to<std::string>, std::hash<std::string>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<true, false, true> >::_Hashtable(unsigned long, std::hash<std::string> const&, std::__detail::_Mod_range_hashing const&, std::__detail::_Default_ranged_hash const&, std::equal_to<std::string> const&, std::__detail::_Select1st const&, std::allocator<std::pair<std::string const, int> > const&) (hashtable.h:799)
==27031== by 0x401BB5: std::_Hashtable<std::string, std::pair<std::string const, int>, std::allocator<std::pair<std::string const, int> >, std::__detail::_Select1st, std::equal_to<std::string>, std::hash<std::string>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<true, false, true> >::_Hashtable(unsigned long, std::hash<std::string> const&, std::equal_to<std::string> const&, std::allocator<std::pair<std::string const, int> > const&) (hashtable.h:420)
==27031== by 0x4018DC: std::unordered_map<std::string, int, std::hash<std::string>, std::equal_to<std::string>, std::allocator<std::pair<std::string const, int> > >::unordered_map(unsigned long, std::hash<std::string> const&, std::equal_to<std::string> const&, std::allocator<std::pair<std::string const, int> > const&) (unordered_map.h:143)
==27031== by 0x401257: BufferManager::BufferManager(unsigned int) (BufferManager.cpp:21)
==27031== by 0x4041B6: main (buffertest.cpp:79)
==27031== Address 0x5c1d0e0 is 16 bytes after a block of size 64 alloc'd
==27031== at 0x4C29180: operator new(unsigned long) (vg_replace_malloc.c:324)
==27031== by 0x4041A3: main (buffertest.cpp:79)
==27031==
==27031== Invalid write of size 8
==27031== at 0x4020A5: std::_Hashtable<std::string, std::pair<std::string const, int>, std::allocator<std::pair<std::string const, int> >, std::__detail::_Select1st, std::equal_to<std::string>, std::hash<std::string>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<true, false, true> >::_Hashtable(unsigned long, std::hash<std::string> const&, std::__detail::_Mod_range_hashing const&, std::__detail::_Default_ranged_hash const&, std::equal_to<std::string> const&, std::__detail::_Select1st const&, std::allocator<std::pair<std::string const, int> > const&) (hashtable.h:799)
==27031== by 0x401BB5: std::_Hashtable<std::string, std::pair<std::string const, int>, std::allocator<std::pair<std::string const, int> >, std::__detail::_Select1st, std::equal_to<std::string>, std::hash<std::string>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<true, false, true> >::_Hashtable(unsigned long, std::hash<std::string> const&, std::equal_to<std::string> const&, std::allocator<std::pair<std::string const, int> > const&) (hashtable.h:420)
==27031== by 0x4018DC: std::unordered_map<std::string, int, std::hash<std::string>, std::equal_to<std::string>, std::allocator<std::pair<std::string const, int> > >::unordered_map(unsigned long, std::hash<std::string> const&, std::equal_to<std::string> const&, std::allocator<std::pair<std::string const, int> > const&) (unordered_map.h:143)
==27031== by 0x401257: BufferManager::BufferManager(unsigned int) (BufferManager.cpp:21)
==27031== by 0x4041B6: main (buffertest.cpp:79)
==27031== Address 0x5c1d0e8 is 24 bytes after a block of size 64 in arena "client"
==27031==
valgrind: m_mallocfree.c:304 (get_bszB_as_is): Assertion 'bszB_lo == bszB_hi' failed.
valgrind: Heap block lo/hi size mismatch: lo = 128, hi = 0.
This is probably caused by your program erroneously writing past the
end of a heap block and corrupting heap metadata. If you fix any
invalid writes reported by Memcheck, this assertion failure will
probably go away. Please try that before reporting this as a bug.
host stacktrace:
==27031== at 0x380A48EF: show_sched_status_wrk (m_libcassert.c:319)
==27031== by 0x380A49E4: report_and_quit (m_libcassert.c:390)
==27031== by 0x380A4B66: vgPlain_assert_fail (m_libcassert.c:455)
==27031== by 0x380B170D: get_bszB_as_is (m_mallocfree.c:302)
==27031== by 0x380B170D: get_bszB (m_mallocfree.c:312)
==27031== by 0x380B170D: get_pszB (m_mallocfree.c:386)
==27031== by 0x380B170D: vgPlain_describe_arena_addr (m_mallocfree.c:1532)
==27031== by 0x3809DC93: vgPlain_describe_addr (m_addrinfo.c:188)
==27031== by 0x3809C73B: vgMemCheck_update_Error_extra (mc_errors.c:1133)
==27031== by 0x380A05BA: vgPlain_maybe_record_error (m_errormgr.c:818)
==27031== by 0x3809BCB2: vgMemCheck_record_address_error (mc_errors.c:753)
==27031== by 0x803EDF459: ???
==27031== by 0x802D95EEF: ???
==27031== by 0x3807295F: vgMemCheck_malloc (mc_malloc_wrappers.c:388)
==27031== by 0x4020A0: std::_Hashtable<std::string, std::pair<std::string const, int>, std::allocator<std::pair<std::string const, int> >, std::__detail::_Select1st, std::equal_to<std::string>, std::hash<std::string>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<true, false, true> >::_Hashtable(unsigned long, std::hash<std::string> const&, std::__detail::_Mod_range_hashing const&, std::__detail::_Default_ranged_hash const&, std::equal_to<std::string> const&, std::__detail::_Select1st const&, std::allocator<std::pair<std::string const, int> > const&) (hashtable.h:799)
sched status:
running_tid=1
Thread 1: status = VgTs_Runnable
==27031== at 0x401682: std::__detail::_Prime_rehash_policy::_Prime_rehash_policy(float) (hashtable_policy.h:464)
==27031== by 0x4020C4: std::_Hashtable<std::string, std::pair<std::string const, int>, std::allocator<std::pair<std::string const, int> >, std::__detail::_Select1st, std::equal_to<std::string>, std::hash<std::string>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<true, false, true> >::_Hashtable(unsigned long, std::hash<std::string> const&, std::__detail::_Mod_range_hashing const&, std::__detail::_Default_ranged_hash const&, std::equal_to<std::string> const&, std::__detail::_Select1st const&, std::allocator<std::pair<std::string const, int> > const&) (hashtable.h:799)
==27031== by 0x401BB5: std::_Hashtable<std::string, std::pair<std::string const, int>, std::allocator<std::pair<std::string const, int> >, std::__detail::_Select1st, std::equal_to<std::string>, std::hash<std::string>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<true, false, true> >::_Hashtable(unsigned long, std::hash<std::string> const&, std::equal_to<std::string> const&, std::allocator<std::pair<std::string const, int> > const&) (hashtable.h:420)
==27031== by 0x4018DC: std::unordered_map<std::string, int, std::hash<std::string>, std::equal_to<std::string>, std::allocator<std::pair<std::string const, int> > >::unordered_map(unsigned long, std::hash<std::string> const&, std::equal_to<std::string> const&, std::allocator<std::pair<std::string const, int> > const&) (unordered_map.h:143)
==27031== by 0x401257: BufferManager::BufferManager(unsigned int) (BufferManager.cpp:21)
==27031== by 0x4041B6: main (buffertest.cpp:79)
Note: see also the FAQ in the source distribution.
It contains workarounds to several common problems.
In particular, if Valgrind aborted or crashed after
identifying problems in your program, there's a good chance
that fixing those problems will prevent Valgrind aborting or
crashing, especially if it happened in m_mallocfree.c.
If that doesn't help, please report this bug to: www.valgrind.org
In the bug report, send all the above text, the valgrind
version, and what OS and version you are using. Thanks.
可悲的是,我无法弄清楚这些错误想告诉我什么。它们发生的代码甚至没有使用此unordered_map做任何事情,它只是构造函数。
我会很感激任何帮助或想法如何解决此问题。
以下是我的代码的来源(相关行)。如果您需要更多信息或希望我进行任何测试,我们将很乐意为您提供。
BufferManager.hpp(已更新)
#ifndef BUFFER_MANAGER_HPP
#include "BufferFrame.hpp"
#include <stdint.h>
#include <unordered_map>
class BufferManager {
public:
BufferManager( const unsigned int pageCount );
BufferFrame& fixPage( const uint64_t pageId, const bool exclusive );
void unfixPage( BufferFrame& frame, const bool isDirty );
~BufferManager();
private:
uint64_t pageCount;
std::unordered_map<uint64_t, BufferFrame *> pages;
std::unordered_map<std::string, int> files;
};
#define BUFFER_MANAGER_HPP
#endif
BufferManager.cpp(部件)(已更新):
BufferManager::BufferManager( const unsigned int pageCount ) {
this->pageCount = pageCount;
std::cout << "BufferFrame constructor" << std::endl;
}
BufferFrame& BufferManager::fixPage( const uint64_t pageId, const bool exclusive ) {
std::cout << "fixPage called with pageId: " << pageId << " requesting exclusive: " << exclusive<< std::endl;
std::cout << "Pages.size(): " << pages.size() << std::endl;
//Test if we got the frame in the buffer
BufferFrame * frame;
std::unordered_map<uint64_t, BufferFrame *>::const_iterator got = pages.find(pageId);
if (got == pages.end()) {
frame = new BufferFrame();
frame->data = malloc(pageSize);
frame->pageId = pageId;
frame->isDirty = false;
std::pair<uint64_t, BufferFrame *> element(pageId, frame);
pages.insert(element);
std::cout << "fixPage of page: " << pageId << " (empty page used. Load not implemented!)" << std::endl;
}
else {
frame = got->second;
std::cout << "fixPage of page: " << pageId << " (Page loaded from map)" << std::endl;
}
return *frame;
}
BufferFrame.hpp:
#ifndef BUFFER_FRAME_HPP
#include <stdint.h>
class BufferFrame {
friend class BufferManager;
public:
void* getData();
private:
void * data;
uint64_t pageId;
bool isDirty;
};
#define BUFFER_FRAME_HPP
#endif
到目前为止,我所尝试的是用BufferFrame *代替unordered_map中的BufferFrame,该FrameFrame *并没有改变。即使将其替换为int似乎也没有任何效果。
编辑:更新了BufferManager.hpp和BufferManager.cpp以使用unordered_map。
EDIT2:首先:谢谢所有尝试帮助我的人
我发现了问题:这是Makefile中的一个错误,导致我的.o文件之一无法重新生成。这导致使用不同版本的BufferFrame.hpp的不同文件。显然,这必定会导致bizzarre内存错误(就像我得到的一样)。
最佳答案
似乎代码正在返回对局部变量的引用。
BufferFrame& BufferManager::fixPage(
....
BufferFrame frame;
....
return frame;
您可能需要将设计更改为
std::unordered_map<uint64_t, BufferFrame *> pages;
甚至
std::unordered_map<uint64_t, std::shared_ptr<BufferFrame> > pages;
我还建议将
BufferFrame
创建为带有构造函数和析构函数的适当C++类,以便它管理自己的内存。