2

我有一些代码来使用模板静态类处理资源(图像、字体、网格数据等)管理,允许客户端代码执行以下操作:

ResourceManager<Texture>::init("data/textures");
ResourceManager<Font>::init("data/fonts");
// later ...
boost::shared_ptr<const Texture> tex = ResourceManager<Texture>::getResource("wall.png");
boost::shared_ptr<const Font> font = ResourceManager<Font>::getResource("Arial.ttf");
// later ...
ResourceManager<Texture>::release();

“资源类型”必须有一个构造函数采用const std::string&.

getResource实现如下:

static boost::shared_ptr<const ResourceType> getResource(const std::string& fileName)
{
    boost::shared_ptr<ResourceType> resource;

    typename table_t::const_iterator itr = _resources.find(fileName);
    if (itr == _resources.end()) {
        resource.reset(new ResourceType(_dataDirectory + fileName));
        _resources[fileName] = resource;
    } else {
        resource = itr->second;
    }

    return resource;
}

table_t定义为typedef typename boost::unordered_map< std::string, boost::shared_ptr<ResourceType> > table_t;

_resources是类型table_t

问题是boost::unordered_map我在调用find(源自find_iterator)时遇到了段错误。但是,std::map相反,我在插入操作(源自_Rb_tree_decrement)或调用find(源自string::compare)时遇到段错误。

该问题仅在第二次请求资源时发生(发生故障时文件名有效)。

由于两者都发生了这种情况,mapunordered_map假设我必须在某个地方做一些奇怪的事情来导致这种情况,有什么想法吗?

谢谢。

编辑:仍然有问题,我错了,它只发生在第二次请求资源时。但是,获取资源的前 2 次调用是成功的,这是导致段错误的第 3 次调用(每次调用都针对不同的资源)。

这是一个堆栈跟踪:

Program received signal SIGSEGV, Segmentation fault.
0x00000000004b4978 in boost::unordered_detail::hash_table<boost::unordered_detail::map<std::string, boost::hash<std::string>, std::equal_to<std::string>, std::allocator<std::pair<std::string const, boost::shared_ptr<Texture> > > > >::find_iterator (this=0x7aed80, bucket=0x38, k=...)
    at /usr/local/include/boost/unordered/detail/table.hpp:55
55          node_ptr it = bucket->next_;
(gdb) bt
#0  0x00000000004b4978 in boost::unordered_detail::hash_table<boost::unordered_detail::map<std::string, boost::hash<std::string>, std::equal_to<std::string>, std::allocator<std::pair<std::string const, boost::shared_ptr<Texture> > > > >::find_iterator (this=0x7aed80, bucket=0x38, k=...)
    at /usr/local/include/boost/unordered/detail/table.hpp:55
#1  0x00000000004b294c in boost::unordered_detail::hash_table<boost::unordered_detail::map<std::string, boost::hash<std::string>, std::equal_to<std::string>, std::allocator<std::pair<std::string const, boost::shared_ptr<Texture> > > > >::find (this=0x7aed80, k=...)
    at /usr/local/include/boost/unordered/detail/table.hpp:583
#2  0x00000000004b07c1 in boost::unordered_map<std::string, boost::shared_ptr<Texture>, boost::hash<std::string>, std::equal_to<std::string>, std::allocator<std::pair<std::string const, boost::shared_ptr<Texture> > > >::find (this=0x7aed80, k=...)
    at /usr/local/include/boost/unordered/unordered_map.hpp:423
#3  0x00000000004ae7c6 in ResourceManager<Texture>::getResource (fileName=...) at /home/tim/Projects/gameproj/app/ResourceManager.hpp:52
#4  0x00000000004ce7fc in Map::loadCellTextures (this=0x7fffffffdfc0, in=...) at /home/tim/Projects/gameproj/app/Map.cpp:57
#5  0x00000000004ce632 in Map (this=0x7fffffffdfc0, fileName=...) at /home/tim/Projects/gameproj/app/Map.cpp:30
#6  0x0000000000495702 in Game::init (xResolution=1024, yResolution=768) at /home/tim/Projects/gameproj/app/Game.cpp:116
#7  0x0000000000494fa0 in Game::run (xResolution=1024, yResolution=768) at /home/tim/Projects/gameproj/app/Game.cpp:38
#8  0x0000000000487f1d in Main::run (xResolution=1024, yResolution=768) at /home/tim/Projects/gameproj/app/Main.cpp:28
#9  0x0000000000487db5 in main (argc=1, argv=0x7fffffffe398) at /home/tim/Projects/gameproj/app/main.cpp:10
4

3 回答 3

2

我看不出任何明显的错误,你试过Valgrind吗(假设你运行某种 *nix 系统)?它是查找内存错误的宝贵工具,看起来它可能就是其中之一。

于 2010-07-08T02:48:14.620 回答
0

看我遇到了类似的问题。我的课程在 BlackBerry 的编译器和 qmake 4 上运行良好。但在 qtcreator 5(64 和 32b)上我遇到了同样的问题。我追踪了这个 http://www.instructables.com/answers/Why-would-an-empty-stdmap-seg-fault-on-the-first/#CXBBGN4GQO92M2Q

看起来默认构造函数在某些编译器上没有正确初始化。我clear()在父类构造函数上添加了一个,现在似乎可以工作了。

如果您有一些静态声明(或更糟糕的外部声明)并且应该实例化地图的时间不是预期的,也会发生这种情况。

我希望它有帮助。

于 2014-07-11T04:27:43.593 回答
0

该问题仅在第二次请求资源时出现

这向我表明您的代码可能正在释放资源 - 第一次一切正常,然后您释放它,然后容器下次尝试访问该内存时,它会出现段错误。

于 2010-07-08T02:45:18.170 回答