增长managed_shared_memory段后出现段错误

时间:2019-09-12 20:17:57

标签: c++ boost memory-management shared-memory

我正在研究boost库的共享内存部分,以准备更大的项目。我需要一个共享的内存段,初始化时不一定知道它的大小,因此我的计划是扩展此段。

我的初始实现有一个boost::interprocess::vector存储在共享内存中。当我向向量添加值时,如果向量的大小超过了该段的大小(抛出bad_alloc异常),则该段会增大。这通常可以工作几次,直到出现细分错误。

不确定这是否有帮助,在实施此方法之前,我的代码会在调用managed_shared_memory::grow()方法时挂起。经过一番谷歌搜索后,我发现这很可能是因为该段仍被映射到主流程。在将remover结构移出构造函数后,发生了当前错误。现在,该代码不会挂起,但是会出现段错误。

代码如下:

#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();
}

这是我得到的典型输出:

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)

解决方案

如oxuf所指出的,重新映射该段,然后使用find可以正常工作。这是更新的功能,以防万一有人偶然发现相同的问题:

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);
    }
}

1 个答案:

答案 0 :(得分:1)

每次增长一个段时,操作系统都可以自由地重新分配整个内存块,然后,指向该内存的所有指针都将变为无效。然后,test_vector可能会在增长后指向无效的内存。尝试再次映射细分,这次使用open_only选项,然后使用managed_shared_memory::find到达您的vector