【问题标题】:Segmentation fault after growing a managed_shared_memory segment增长 managed_shared_memory 段后出现分段错误
【发布时间】:2019-09-12 20:17:57
【问题描述】:

我正在使用 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);
    }
}

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2014-04-02
      • 1970-01-01
      • 2015-02-16
      • 2020-03-24
      • 2019-06-13
      • 2013-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多