我是新手来提升。 我在下面的示例中使用“boost managed shared memory”,但是在下一行的共享段中分配内存时,一个实例崩溃了:
char_string key_object(keyHashStr.c_str(), alloc_inst3);
只有在我的示例应用程序的多个实例同时运行时才会发生崩溃。 如果我使用“boost managed windows shared memory”那么就没有崩溃。
有人可以让我知道我做错了什么吗?
#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;
}
}
}
答案 0 :(得分:1)
我也有类似的问题。这个过程在内部崩溃了。在我的情况下,当两个进程试图同时创建或打开一个特定的共享内存区域时,其中一个进程随后在“for”循环中崩溃。 我在创建共享内存区域时使用了“boost named mutex”。这对我有用。如果我们采用sehe的代码,那么我认为以下修改可以解决问题。
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/sync/named_mutex.hpp>
#include <string>
#include <iostream>
#define RESERVED_BUFFER_SIZE_WRITE (8 * 0x0100000)
namespace bip = boost::interprocess;
//Typedefs of allocators and containers
typedef bip::allocator<char, bip::managed_shared_memory::segment_manager> char_allocator;
typedef bip::basic_string<char, std::char_traits<char>, char_allocator> char_string;
int main()
{
boost::interprocess::named_mutex MyBookkeeperMutex(boost::interprocess::open_or_create, "MyBookkeeperMutex");
MyBookkeeperMutex.lock();
bip::managed_shared_memory m_sharedMemUsage(bip::open_or_create, "MyBookkeeper", 2 * RESERVED_BUFFER_SIZE_WRITE);
MyBookkeeperMutex.unlock();
char_allocator alloc_inst3(m_sharedMemUsage.get_segment_manager());
for (int count = 0; count < 100000; ++count) {
char_string key_obect("AAAAAAAAAAAAAAAAAAAAAAA", alloc_inst3);
}
boost::interprocess::named_mutex::remove("MyBookkeeperMutex");
}
答案 1 :(得分:0)
我建议改为使用open_or_create
标志。
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <string>
#include <iostream>
#define RESERVED_BUFFER_SIZE_WRITE (8 * 0x0100000)
namespace bip = boost::interprocess;
//Typedefs of allocators and containers
typedef bip::allocator<char, bip::managed_shared_memory::segment_manager> char_allocator;
typedef bip::basic_string<char, std::char_traits<char>, char_allocator> char_string;
int main()
{
bip::managed_shared_memory m_sharedMemUsage(bip::open_or_create, "MyBookkeeper", 2 * RESERVED_BUFFER_SIZE_WRITE);
char_allocator alloc_inst3(m_sharedMemUsage.get_segment_manager());
for (int count = 0; count < 100000; ++count) {
char_string key_obect("AAAAAAAAAAAAAAAAAAAAAAA", alloc_inst3);
}
}
注意这不会泄漏资源,因为类的析构函数实际运行...