Reputation: 45
I'm playing around with the shared memory portion of the boost library in preparation for a bigger project. I need a shared memory segment, the size of which I won't necessarily know at initialization, so my plan is to grow this segment.
My initial implementation has a boost::interprocess::vector
stored in shared memory. As I add values to the vector, if the size of the vector outgrows the segment (a bad_alloc
exception is thrown), I grow the segment. This usually works a few times, until I get a segmentation fault.
Not sure if this helps at all, before this implementation, my code would hang upon invoking the managed_shared_memory::grow()
method. After some googling, I figured out it was likely because the segment was still mapped to the main process. The current error occurred after I moved the remover
struct out of the constructor. Now, the code doesn't hang, but it does segfault.
Here is the code:
#include <iostream>
#include <exception>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
using namespace boost::interprocess;
static const std::string SHMEM_NAME = "MySharedMemorySegment";
typedef allocator<int, managed_shared_memory::segment_manager> IntAllocator;
typedef vector<int, IntAllocator> ShmemVector;
class ShmemTest {
private:
managed_shared_memory *segment;
const IntAllocator *allocator_instance;
ShmemVector *test_vector;
size_t shmem_block = 10000; // Originally, I could allocate 65536 without problems
struct shm_remove {
shm_remove() { shared_memory_object::remove(SHMEM_NAME.data()); }
~shm_remove() { shared_memory_object::remove(SHMEM_NAME.data()); }
} remover;
public:
ShmemTest() {
segment = new managed_shared_memory(create_only, SHMEM_NAME.data(), shmem_block);
allocator_instance = new IntAllocator(segment->get_segment_manager());
}
void create_vector() {
test_vector = segment->construct<ShmemVector>("ShmemVector")(*allocator_instance);
}
void append(int value) {
try {
test_vector->push_back(value);
} catch (std::exception & e) { // Grow shared memory if out of memory
std::cout << e.what() << std::endl;
std::cout << "Growing the shared memory block" << std::endl;
managed_shared_memory::grow(SHMEM_NAME.data(), 1000);
std::cout << "Done growing the shared memory block, current size: " << size();
std::cout << " max size: " << max_size() << std::endl;
test_vector->push_back(value);
std::cout << "Added value after growing shared memory block" << std::endl;
}
}
void print() {
std::cout << "ShmemVector values: ";
for (int value : *test_vector) {
std::cout << " " << value << " ";
}
std::cout << std::endl;
}
size_t max_size() {
return test_vector->max_size();
}
size_t size() {
return test_vector->size();
}
void destroy() {
segment->destroy<ShmemVector>("ShmemVector");
}
};
int main() {
ShmemTest shmem_test;
shmem_test.create_vector();
std::cout << "Max size: " << shmem_test.max_size() << std::endl;
for (int i = 0; i < 16380; ++i) {
shmem_test.append(i);
if (i > 16200) {
std::cout << "Current size: " << shmem_test.size() << " ";
}
}
std::cout << std::endl;
shmem_test.print();
shmem_test.destroy();
}
This is the typical output I get:
Max size: 2496
boost::interprocess::bad_alloc
Growing the shared memory block
Done growing the shared memory block, current size: 2414 max size: 2746
Added value after growing shared memory block
boost::interprocess::bad_alloc
Growing the shared memory block
Done growing the shared memory block, current size: 2662 max size: 2996
Added value after growing shared memory block
boost::interprocess::bad_alloc
Growing the shared memory block
Done growing the shared memory block, current size: 2914 max size: 3246
Segmentation fault (core dumped)
As pointed out by oxuf, re-mapping the segment and then using find
worked. Here is the updated function, in case anyone stumbles upon the same problem:
void append(int value) {
try {
test_vector->push_back(value);
} catch (std::exception & e) { // Grow shared memory if out of memory
managed_shared_memory::grow(SHMEM_NAME.data(), 1000);
// Re-map the shared memory segment
segment = new managed_shared_memory(open_only, SHMEM_NAME.data());
// Find the vector object in the newly mapped shared memory segment
std::pair<ShmemVector*, size_t> return_value = segment->find<ShmemVector>("ShmemVector");
test_vector = return_value.first;
// Append the value
test_vector->push_back(value);
}
}
Upvotes: 1
Views: 577
Reputation: 1467
Every time a segment is grown, the OS is free to reallocate the whole memory chunk, and then, all pointers to that memory become invalid. Then, test_vector
could point to invalid memory after growing. Try mapping the segment again, this time with the open_only
option and use managed_shared_memory::find
to reach to your vector
.
Upvotes: 1