[Boost-bugs] [Boost C++ Libraries] #3826: shared_ptr has incorrect use_count when compiled -O2

Subject: [Boost-bugs] [Boost C++ Libraries] #3826: shared_ptr has incorrect use_count when compiled -O2
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2010-01-07 13:21:59


#3826: shared_ptr has incorrect use_count when compiled -O2
------------------------------------------------+---------------------------
 Reporter: Sean Munro <smunro@…> | Owner: pdimov
     Type: Bugs | Status: new
Milestone: Boost 1.42.0 | Component: smart_ptr
  Version: Boost 1.41.0 | Severity: Problem
 Keywords: shared_ptr |
------------------------------------------------+---------------------------
 We've come across an issue in boost 1.41 that didn't exist in boost 1.38
 with shared_ptr.[[BR]]
 We came across instances where an assignment of the shared_ptr did not
 increase the use_count(). Strangely, if you put the assignment in a loop,
 after multiple attempts it may eventually have the correct
 use_count.[[BR]]
 I've attached a simple program that shows the problem.
 This problem occurs only when using the -O2 or -O3 flag with g++.[[BR]]

 We use this version of g++ (on RedHat Enterprise 4 Update 6):
 {{{
 g++ (GCC) 3.4.6 20060404 (Red Hat 3.4.6-9)
 }}}

 The attached program outputs this:
 FAILED AFTER SET
 pApple:0x8b27008 count= 1
 mpFruit:0x8b27008 count= 1

 To build the test program, do:


 {{{
 g++ -O2 -c -o shared_ptr_test.o shared_ptr_test.cpp
 g++ shared_ptr_test.o -o shared_ptr_test.exc
 }}}

 Test program(shared_ptr_test.cpp):
 {{{
 #include <iostream>
 #include <boost/shared_ptr.hpp>

 using namespace std;
 using namespace boost;

 class Fruit
 {
     public:
         Fruit() : mBaseStr() {};

         string mBaseStr;
 };
 typedef boost::shared_ptr<Fruit> FruitPtr;

 class Apple : public Fruit
 {
     public:
         Apple() : Fruit(), mOtherStr() {};

         string mOtherStr;
 };

 typedef boost::shared_ptr<Apple> ApplePtr;

 class Container
 {
     public:
         void SetFruit(const FruitPtr &pFruit)
         {
             mpFruit = pFruit;
         }
         void SetApple(const ApplePtr &pApple)
         {
             mpFruit = pApple;
         }

         void Create()
         {
             ApplePtr pApple(new Apple);
             // SetApple(pApple) is ok
             SetFruit(pApple);

             if (pApple.use_count() != 2)
             {
                 cout << "FAILED AFTER SET" << endl;
                 cout << "pApple:" << pApple.get() << " count= "
                      << pApple.use_count() << endl;
                 cout << "mpFruit:" << mpFruit.get() << " count= "
                      << mpFruit.use_count() << endl;
             }
             pApple.reset();
             // this->mpFruit is now pointing to free'd memory and will
 segfault
         }

     private:
         FruitPtr mpFruit;
 };


 int main(int argc, char *argv[])
 {
     Container Tester;

     Tester.Create();

     return 0;
 }
 }}}

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/3826>
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