-2

I have a following boost::interprocess::map:

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

I have a method() which inserts into this map:

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

}

And in main(), I call it like this:

int main()
{
    InsertInMap(1);
}

This compiles fine.

But when I run my program, I get the following run-time error at the marked line (while insertion):

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

Am I missing something?

CinCout
  • 9,486
  • 12
  • 49
  • 67
  • 2
    This has nothing to do with boost, interprocess or maps. Initialize your variables. And **don't `new` all the time** (this is not Java!) – sehe Oct 24 '14 at 09:31
  • Well, if you are pointing towards the `new` in `InsertInMap()` above, then that is required since I want a new memory for each `i` passed! As far as the error is concerned, I have realized the silly mistake I made! – CinCout Oct 24 '14 at 11:24

1 Answers1

2

You need to do

myMap = boost::shared_ptr< map<uint32, managed_shared_memory*> > (new map<uint32, managed_shared_memory*> );

to allocate the memory for the shared pointer before you use it to add to the map. You are getting this error because myMap is conceptually the same as a null pointer until you allocate memory for it.