I am new to boost. I am using “boost managed shared memory” in the following sample but one instance gets crashed while allocating memory in the shared segment on the following line :
char_string key_object(keyHashStr.c_str(), alloc_inst3);
The crash occurs only if more than one instance of my sample application are running at the same time. If I use “boost managed windows shared memory” then there is no crash.
Can someone please let me know what I am doing wrong ?
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/flat_map.hpp>
#include <boost/interprocess/containers/set.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <string>
#include <Windows.h>
#include <iostream>
#define AMMSRCUNITLIMIT 0x0100000 /* 1M bytes */
#define RESERVED_BUFFER_SIZE_WRITE (8 * AMMSRCUNITLIMIT)
#define SHM_SIZE_FOR_TRACKING 65536
#define MAX_PATH 260
using namespace boost::interprocess;
//Typedefs of allocators and containers
typedef allocator<void, managed_shared_memory::segment_manager> void_allocator;
typedef allocator<char, managed_shared_memory::segment_manager> char_allocator;
typedef boost::interprocess::basic_string<char, std::char_traits<char>, char_allocator> char_string;
int main(int argc, char *argv[])
{
managed_shared_memory *m_sharedMemUsage = NULL;
string keyHashStr;
try
{
m_sharedMemUsage = new managed_shared_memory(create_only, "MyBookkeeper", 2 * RESERVED_BUFFER_SIZE_WRITE);
keyHashStr = "AAAAAAAAAAAAAAAAAAAAAAA";
}
catch (...)
{
while (true)
{
try{
m_sharedMemUsage = new managed_shared_memory(open_only, "MyBookkeeper");
break;
}
catch (...)
{
std::cout << "Some problem, trying again" << std::endl;
}
}
keyHashStr = "AAAAAAAAAAAAAAAAAAAAAAB";
}
{
char_allocator alloc_inst3 = m_sharedMemUsage->get_segment_manager()->get_allocator<char>();
int count = 0;
while (count < 100000)
{
char_string key_object(keyHashStr.c_str(), alloc_inst3);
++count;
}
}
}