【问题标题】:Inserting in a boost::interprocess::map插入 boost::interprocess::map
【发布时间】:2014-10-24 08:45:56
【问题描述】:

我有一个关注者boost::interprocess::map

using boost::interprocess;  
std::shared_ptr< map<uint32, managed_shared_memory*> > myMap;

我有一个方法()插入到这个地图中:

void InsertInMap(uint32 i)
{
    myMap->insert( std::make_pair (i,                       // runtime error here
               new managed_shared_memory(create_only, "Test", 256)) );    

}

main(),我这样称呼它:

int main()
{
    InsertInMap(1);
}

这编译得很好。

但是当我运行我的程序时,我在标记的行(插入时)收到以下运行时错误:

memory access violation occurred at address ......, while attempting to read inaccessible data

我错过了什么吗?

【问题讨论】:

  • 这与 boost、interprocess 或 maps 无关。初始化变量。而且不要一直new(这不是Java!)
  • 好吧,如果您指向上面InsertInMap() 中的new,那么这是必需的,因为我希望为每个传递的i 提供一个新内存!就错误而言,我已经意识到我犯的愚蠢错误!

标签: c++ boost boost-interprocess


【解决方案1】:

你需要做的

myMap = boost::shared_ptr&lt; map&lt;uint32, managed_shared_memory*&gt; &gt; (new map&lt;uint32, managed_shared_memory*&gt; );

在使用共享指针添加到映射之前为共享指针分配内存。您收到此错误是因为 myMap 在概念上与空指针相同,直到您为其分配内存。

【讨论】:

  • 或者..根本不使用动态分配
  • 抱歉这么愚蠢的问题!我想知道我是怎么错过的。我猜是节日的影响;)
猜你喜欢
  • 2013-09-20
  • 1970-01-01
  • 2021-12-24
  • 1970-01-01
  • 1970-01-01
  • 2021-06-09
  • 1970-01-01
  • 1970-01-01
  • 2018-06-24
相关资源
最近更新 更多