boostorg / boostorg/boost

Boost >= 1.87 throws bad_alloc when allocating memory for a 64-byte aligned data structure using managed_shared_memory, even though there is sufficient free memory

Open
#1,041 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
HTML
Stars
8.6k
Forks
1.9k
Avg merge
39m
Merged PRs (30d)
2

Description

Hi there, I haven't tested this with Boost 1.86, jumped straight from 1.85 to 1.87. The code snippet below works well in Boost 1.85, but when using 1.87 or 1.88, I noticed it fails and throw bad_alloc. Some changes related to memory_algorithm seem to break it. While debugging, I found that the real_size calculated in [allocate_aligned](https://github.com/boostorg/interprocess/blob/3ae98a5f0d85814bcb646839fc4814a9307bcb89/include/boost/interprocess/mem_algo/detail/mem_algo_common.hpp) is larger than the currently available free memory. I'm also wondering if I'm using it incorrectly. Please advise — thanks!

```
#include
#include "boost/interprocess/interprocess_fwd.hpp"
#include "boost/interprocess/managed_shared_memory.hpp"
#include "boost/interprocess/shared_memory_object.hpp"

template
class MyClass {
public:
static_assert((N & (N - 1)) == 0, "size must be power of 2");

private:
alignas(64) std::array data_;
};

namespace bip = boost::interprocess;

int main() {
constexpr auto kName = "TestShm";
bip::shared_memory_object::remove(kName);
try {
auto shm = bip::managed_shared_memory(bip::open_or_create, kName,
65536);
auto free_mem = shm.get_free_memory();
std::cout << "Free mem: " << free_mem << std::endl;
using Data = MyClass<32768>;
[[maybe_unused]] auto data = shm.find_or_construct("Data")();
}
catch (const std::exception& e) {
std::cout << "Fail, " << e.what() << std::endl;
bip::shared_memory_object::remove(kName);
return 1;
}

bip::shared_memory_object::remove(kName);
return 0;
}

```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.