#include namespace fs = boost::filesystem; #include #include #include #include namespace ip = boost::interprocess; typedef ip::basic_managed_mapped_file, ip::flat_map_index> mfile_object; typedef mfile_object::segment_manager SegmentManager; class Node { public: typedef ip::allocator NodeAllocator; typedef ip::vector NodeVector; explicit Node(const NodeAllocator& allocator) : nodes_(allocator) {} NodeVector nodes_; }; // Create a chunk of shared memory on disk (memory mapped file) consisting of multiple nodes in a tree-like structure. // Test shared memory file creation on Windows 7, VS2010 and VS2013, Boost 1.55.0 vs Boost 1.56.0, x64 // Test works fine on Boost 1.55.0 (and presumably before since this example is based on our existing code), crashes on Boost 1.56.0. // Using vector reserve(SIZE) fixes the crash for this example. // Error reported: Assertion failed: ptr != 0, file D:\boost64\include\boost-1_56\boost/interprocess/allocators/allocator.hpp, line 268 int main(int argc, char* argv[]) { const std::string filePath = "test.mmf"; if (fs::exists(filePath)) fs::remove(filePath); const unsigned int size = 256 * 1024; mfile_object* mmf = new mfile_object(ip::open_or_create, filePath.c_str(), size); Node::NodeAllocator allocator(mmf->get_segment_manager()); Node *rootNode = mmf->construct(ip::unique_instance)(allocator); // Breaks at 43 during testing (42 and below is fine for this example). const int MaxItems = 43; // [NOTE: reserving required space prevents crash in this case] rootNode->nodes_.reserve(MaxItems); for (int i = 0; i < MaxItems; ++i) { rootNode->nodes_.push_back(Node(allocator)); for (int j = 0; j < MaxItems; ++j) rootNode->nodes_.back().nodes_.push_back(Node(allocator)); } mmf->flush(); delete mmf; mfile_object::shrink_to_fit(filePath.c_str()); return 0; }