find() 或 insert() 上的 C++ unordered_map SIGSEGV
Posted
技术标签:
【中文标题】find() 或 insert() 上的 C++ unordered_map SIGSEGV【英文标题】:C++ unordered_map SIGSEGV on find() or insert() 【发布时间】:2015-04-29 22:59:10 【问题描述】:目前,我正在尝试为我们需要为大学构建的数据库系统实现某种基本的文件访问层。为此,我被要求在内存中缓存页面。为了快速确定哪些页面已经加载,我将它们全部存储在一个 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,这并没有改变任何东西。即使用 int 替换它似乎也没有任何效果。
编辑:更新了 BufferManager.hpp 和 BufferManager.cpp 以使用 unordered_map。
EDIT2:首先:感谢所有试图帮助我的人
我发现了问题:Makefile 中的一个错误导致我的一个 .o 文件无法重新生成。这会导致不同的文件使用不同版本的 BufferFrame.hpp。这显然会导致奇怪的记忆错误(就像我得到的那样)。
【问题讨论】:
【参考方案1】:似乎代码正在返回对局部变量的引用。
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++ 类,以便它管理自己的内存。
【讨论】:
这实际上是我的第一个想法。我只删除了指针,因为我在调试时没有想法。无论如何你是对的,它不能那样工作。但是改回来并没有改变错误。稍后我会将 BufferFrame 更改为适当的 C++ 类,但这不应该是问题。无论如何,如果它确实改变了什么,我会告诉你的。【参考方案2】:请仔细看这两方面:
'pageSize' 设置在哪里以及设置为什么值?我什么都看不到。 在分配“frame”时,您真的需要混合“new”和 malloc。 您没有说明如何在使用后清理 BufferFrame 对象。 如果你在做 new+free 或 malloc+delete,你就有麻烦了。按照 Arun 的建议,我会为 BufferFrame 使用 ctor 和 dtor。
【讨论】:
以上是关于find() 或 insert() 上的 C++ unordered_map SIGSEGV的主要内容,如果未能解决你的问题,请参考以下文章
数据结构搜索二叉树的(递归与非递归)实现,包括:增Insert,删Remove,查Find