[Boost-bugs] [Boost C++ Libraries] #3950: boost::interprocess::mapped_region destructor deletes shm where it shouldn't.

Subject: [Boost-bugs] [Boost C++ Libraries] #3950: boost::interprocess::mapped_region destructor deletes shm where it shouldn't.
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2010-02-20 16:23:51


#3950: boost::interprocess::mapped_region destructor deletes shm where it
shouldn't.
-------------------------------------------+--------------------------------
 Reporter: Lénaïc Huard <lhuard@…> | Owner: igaztanaga
     Type: Bugs | Status: new
Milestone: Boost 1.43.0 | Component: interprocess
  Version: Boost 1.42.0 | Severity: Problem
 Keywords: |
-------------------------------------------+--------------------------------
 Hello,

 I have troubles with the following piece of code:

 {{{
 #include <cstdlib>
 #include <boost/interprocess/anonymous_shared_memory.hpp>
 #include <boost/interprocess/mapped_region.hpp>

 int main()
 {
   boost::interprocess::mapped_region aRegion(
 boost::interprocess::anonymous_shared_memory( sizeof( int ) ) );
   *(int *)aRegion.get_address() = 0x33;

   return EXIT_SUCCESS;
 }
 }}}

 It should create an anonymous memory region and writes an int in it.

 Whereas this piece of code works when compiled with gcc without
 optimizations, when it is compiled with -O2 or -O3 with gcc 4.4.3, it
 produces a segmentation fault.

 strace and gdb show that the shared memory is mmapped and unmapped before
 the assignment is done.
 (Whereas when the code is compiled without optimization, the shared memory
 is clearly mapped, then the assignement is done, then it is unmapped.)

 My understanding of what happens is that:
   - the anonymous_shared_memory() function calls
 raw_mapped_region_creator::create_posix_mapped_region which creates a
 local mapped_region object.
   - This mapped_region object is non-copyable, but movable.
 When the return statement for that local object is called, it invokes
 mapped_region::mapped_region(BOOST_INTERPROCESS_RV_REF(mapped_region)
 other)
 which:
     - create the returned object as invalid,
     - swap the content of the local object with the one of the "invalid
 initialized" returned object.
 Thanks to this, the returned object has the proper values and the local
 object is set as "invalid" just before its destructor is called so that
 the shm won't be unmapped at
 raw_mapped_region_creator::create_posix_mapped_region and
 anonymous_shared_memory exit.

 For a reason I don't understand, I have the feeling that, when the code is
 compiled with gcc 4.4.3 -O2 or -O3, this "swap with invalid mapped_region"
 mechanism is zapped out so that the shm mmapped at the beginning of
 anonymous_shared_memory is unmapped by ~mapped_region when this function
 returns.

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/3950>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:02 UTC