如果在 Windows 上使用 boost managed_shared_memory 会崩溃
Crash if boost managed_shared_memory is used on Windows
我是新手。
我在以下示例中使用了“提升托管共享内存”,但是在以下行的共享段中分配内存时,一个实例崩溃了:
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;
}
}
}
我建议改用 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);
}
}
请注意这不会泄漏资源,因为 class 的析构函数实际运行...
我也遇到了类似的问题。该过程在 boost 内部崩溃。在我的例子中,当两个进程试图同时创建或打开一个特定的共享内存区域时,其中一个进程随后在 "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");
}
我是新手。 我在以下示例中使用了“提升托管共享内存”,但是在以下行的共享段中分配内存时,一个实例崩溃了:
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;
}
}
}
我建议改用 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);
}
}
请注意这不会泄漏资源,因为 class 的析构函数实际运行...
我也遇到了类似的问题。该过程在 boost 内部崩溃。在我的例子中,当两个进程试图同时创建或打开一个特定的共享内存区域时,其中一个进程随后在 "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");
}