Skip to content

Instantly share code, notes, and snippets.

@DonOregano
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DonOregano/10116264 to your computer and use it in GitHub Desktop.
Save DonOregano/10116264 to your computer and use it in GitHub Desktop.
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
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment