Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Short program demonstrating bug in boost containers.
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <iostream>
template <class T>
struct VectorShm
{
typedef boost::interprocess::allocator<T, boost::interprocess::managed_shared_memory::segment_manager> Allocator;
typedef boost::interprocess::vector<T, Allocator> Type;
};
typedef boost::interprocess::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::managed_shared_memory::segment_manager> > StringShm;
typedef VectorShm<StringShm>::Type StringVectorShm;
int main()
{
using namespace boost::interprocess;
//Create a managed shared memory segment
managed_shared_memory segment(open_or_create, "MySharedMemory", 65536);
StringVectorShm v(segment.get_segment_manager());
{
StringShm str("OnRevokedRegistration", segment.get_segment_manager());
v.push_back(str);
}
{
StringShm str("OnCompletedRegistration", segment.get_segment_manager());;
v.push_back(str);
}
{
StringShm str("OnCreateRequest", segment.get_segment_manager());;
v.push_back(str);
}
{
StringShm str("OnUpdateRequest", segment.get_segment_manager());;
v.push_back(str);
}
bool error = false;
for(auto& s : v)
{
if (strlen(s.c_str()) != s.size())
{
std::cout << "Incorrectly terminated string '" << s.c_str() << "'\n"
<< " size() is " << s.size()
<< "\n but strlen is " << strlen(s.c_str()) << std::endl;
error = true;
}
}
shared_memory_object::remove("MySharedMemory");
if (error)
{
std::cout << "Found incorrectly copied/moved string!" << std::endl;
}
else
{
std::cout << "Failed to reproduce error, sorry..." << std::endl;
}
return 0;
}
cmake_minimum_required(VERSION 2.8)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -std=c++11")
ADD_EXECUTABLE(container_problem Boost_container_problem.cpp)
TARGET_LINK_LIBRARIES(container_problem rt)
@DonOregano

This comment has been minimized.

Copy link
Owner Author

@DonOregano DonOregano commented Apr 8, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.