1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2006-2009. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
10 #include <boost/interprocess/detail/config_begin.hpp>
11 #include <boost/interprocess/detail/workaround.hpp>
13 #include <boost/interprocess/managed_shared_memory.hpp>
14 #include <boost/interprocess/smart_ptr/scoped_ptr.hpp>
16 #include "../test/get_process_id_name.hpp"
19 using namespace boost::interprocess;
27 //A functor that destroys the shared memory object
32 //A typedef to save typing
33 typedef managed_shared_memory::segment_manager segment_manager;
34 //This my_deleter is created in the stack, not in shared memory,
35 //so we can use raw pointers
36 segment_manager *mp_segment_manager;
39 //This typedef will specify the pointer type that
40 //scoped_ptr will store
43 my_deleter(segment_manager *s_mngr)
44 : mp_segment_manager(s_mngr){}
46 void operator()(pointer object_to_delete)
47 { mp_segment_manager->destroy_ptr(object_to_delete); }
52 //Create shared memory
53 //Remove shared memory on construction and destruction
58 shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
59 ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
62 shm_remove() { shared_memory_object::remove("MySharedMemory"); }
63 ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
71 managed_shared_memory shmem(create_only, test::get_process_id_name(), 10000);
74 managed_shared_memory shmem(create_only, "MySharedMemory", 10000);
79 //In the first try, there will be no exceptions
80 //in the second try we will throw an exception
81 for(int i = 0; i < 2; ++i){
82 //Create an object in shared memory
83 my_class * my_object = shmem.construct<my_class>("my_object")();
84 my_class * my_object2 = shmem.construct<my_class>(anonymous_instance)();
85 shmem.destroy_ptr(my_object2);
87 //Since the next shared memory allocation can throw
88 //assign it to a scoped_ptr so that if an exception occurs
89 //we destroy the object automatically
90 my_deleter<my_class> d(shmem.get_segment_manager());
93 scoped_ptr<my_class, my_deleter<my_class> > s_ptr(my_object, d);
94 //Let's emulate a exception capable operation
95 //In the second try, throw an exception
97 throw(my_exception());
99 //If we have passed the dangerous zone
100 //we can release the scoped pointer
101 //to avoid destruction
104 catch(const my_exception &){}
105 //Here, scoped_ptr is destroyed
106 //so it we haven't thrown an exception
107 //the object should be there, otherwise, destroyed
109 //Make sure the object is alive
110 if(!shmem.find<my_class>("my_object").first){
113 //Now we can use it and delete it manually
114 shmem.destroy<my_class>("my_object");
117 //Make sure the object has been deleted
118 if(shmem.find<my_class>("my_object").first){
126 #include <boost/interprocess/detail/config_end.hpp>