I'm trying to solve this problem from several days.
In this program there is an unordered_map
which from string key to structure value. The idea works, but when I put the unordered_map
in a class and then access it more easily I get an error:
Eliminare.exe!std::_Hash<std::_Umap_traits<std::string,Data,std::_Uhash_compare<std::string,std::hash<std::string>,std::equal_to<std::string>>,boost::interprocess::allocator<std::pair<std::string,Data>,boost::interprocess::segment_manager<char,boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family,boost::interprocess::offset_ptr<void,__int64,unsigned __int64,0>,0>,boost::interprocess::iset_index>>,0>>::_Find_last<std::string>(const std::string & _Keyval, const unsigned __int64 _Hashval) Row 1510 C++
Eliminare.exe!std::unordered_map<std::string,Data,std::hash<std::string>,std::equal_to<std::string>,boost::interprocess::allocator<std::pair<std::string,Data>,boost::interprocess::segment_manager<char,boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family,boost::interprocess::offset_ptr<void,__int64,unsigned __int64,0>,0>,boost::interprocess::iset_index>>>::at(const std::string & _Keyval) Riga 387 C++
Eliminare.exe!MapCreator::getValue(std::string Key) Row 37 C++
Eliminare.exe!main() Row 7 C++
This is the main:
#include "MapCreator.h"
int main() {
MapCreator mappaClass;
auto values = mappaClass.getValue("value");
}
While this is in MapCreator.h:
#pragma once
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <unordered_map>
#include<iostream>
typedef struct Data
{
int a;
int b;
}dataClass;
namespace bost = boost::interprocess;
typedef std::string KeyType;
typedef dataClass MappedType;
typedef std::pair<std::string, dataClass> ValueType;
typedef bost::allocator<ValueType, bost::managed_shared_memory::segment_manager> ShmemAllocator;
typedef std::unordered_map<KeyType, MappedType, std::hash<KeyType>, std::equal_to<KeyType>, ShmemAllocator> MySHMMap;
class MapCreator
{
public:
MySHMMap* mappa = nullptr;
MapCreator() {
bost::shared_memory_object::remove(nameMemory);
bost::managed_shared_memory segment(bost::create_only, nameMemory, sizeDeclared);
ShmemAllocator alloc_inst(segment.get_segment_manager());
mappa = segment.construct<MySHMMap>("MySHMMapName")(alloc_inst);
dataClass value;
value.a = 12;
value.b = 1111;
mappa->insert(std::pair<std::string, dataClass>("value", value));
std::cout << mappa->at("value").a;
}
dataClass getValue(std::string Key) {
return mappa->at(Key);
}
private:
const int sizeDeclared = 65536;
const char nameMemory[20] = "SharedMemoryName";
};
The "cout
" in the MapCreator
constructor correctly prints "12", but using the getValue
function, it prints the error mentioned above.
Thanks for your help, if you have any questions about the code, just ask.
INFO:
- Compiler: Visual C++ with Visual Studio 2022
- the code without the class works well
- the version of boost class is: 1.78.0