What is the correct way to dispose of a C++ object created with boost::interprocess STL compatible shared memory allocators?

StackOverflow https://stackoverflow.com/questions/15465786

  •  24-03-2022
  •  | 
  •  

Question

I'm a little confused about Boost::interprocess shared memory and deallocation.

When creating a new object on the stack in C++, you simply declare the type, the variable name, and the parameters to the constructor (unless you want a default constructor):

 AType AVariableName(param1,param2); 

If that same variable is to be allocated using Boost::interprocess I am doing something like this. I apologize for the dense series of typedefs but I can't think how to ask this question and be specific without giving real boost templated type expansions.

typedef int                   PointKeyType;
typedef DATAPOINT                 PointMappedType;
typedef std::pair<const int, PointMappedType> PointValueType;
typedef boost::interprocess::allocator<PointValueType, 
  boost::interprocess::managed_shared_memory::segment_manager> PointShmemAllocator;
typedef boost::interprocess::map<PointKeyType, 
  PointMappedType, std::less<PointKeyType>, PointShmemAllocator> PointMap;

So far above all I've done is define that I'm making an <integer,DATAPOINT> std::map-like entity in shared memory and I haven't said what DATAPOINT is, but it's a struct defined with some float, and integer values in it. In the application that owns and initial creates the shared memory map I initialize it like this, mostly borrowed code from a Boost demo:

   managed_shared_memory segment
      ( open_or_create  // or maybe it should be create_only?
       ,MEMORY_AREA_NAME // a string literal
       ,65536);          //segment size in bytes


   // STL compatible allocator
   PointShmemAllocator point_alloc_inst (segment.get_segment_manager());

Now I use the allocator like this, creating boost::interprocess::map<...> using the typedef PointMap:

 PointMap *aPointMap = 
       segment.construct<PointMap>("SOMEOBJECT_NAME_HERE")                                  (std::less<PointKeyType>() //first  ctor parameter
                                 ,point_alloc_inst);     

Now suppose I want to dispose of this boost::interprocess::map object I call a PointMap, and reuse its memory. How do I do that?

I tried something like this:

segment.destruct<PointMap>(aPointMap);

But the syntax is not exactly orthogonal here. Then I I thought, maybe it's some thing like the placement syntax of destructors, but I haven't been able to figure it out.

If it's all magic and it just works, and I am just supposed to just delete PointMap, and that's all there is to it, I'll feel a bit silly, but I want to make sure I'm not making a big mistake.

Secondly, I am assuming that the secondary processes that access this shared memory are simply handled the same way, but I would use the find<T> methods like this:

      std::pair<PointMap*, std::size_t> f = segment->find<PointMap>("SOMEOBJECT_NAME_HERE");
      aPointMap = f.first;

And then, when I'm done with it, delete aPointMap or just set aPointMap = NULL?

Was it helpful?

Solution

See the docs which give a complete example of what you want to do. The map example doesn't destroy the container, but the vectors one does.

PointMap *aPointMap =
       segment.construct<PointMap>("SOMEOBJECT_NAME_HERE")
                                 (std::less<PointKeyType>() //first  ctor parameter
                                 ,point_alloc_inst);

That creates an object named "SOMEOBJECT_NAME_HERE" so to destroy it you just destroy that named object:

segment.destroy<PointMap>("SOMEOBJECT_NAME_HERE");

This is arguably orthogonal: you create an object by name and pass arguments, then destroy it by name (but don't need arguments because destructors don't take arguments.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top