Boost logo

Boost :

From: Thomas Maeder (maeder_at_[hidden])
Date: 2001-05-16 02:29:45

This problem report is a result of a discussion in

The following program demonstrates it, using boost 1.21.1 and gcc 2.95.3:

#include <iostream>

#include "boost/smart_ptr.hpp"

struct S
      std::cout << "~S()\n";
    boost::shared_ptr<S> next;

int main()
  boost::shared_ptr<S> head(new S);
  head->next.reset(new S);
  std::cout << "before assignment\n";
  head = head->next;
  std::cout << "after assignment\n";

The output is

before assginment
after assginment

while one would expect it to be

before assignment
after assignment

The reason is that the shared_ptr copy-assignment operator first reduces the
reference count of the previously owned object and only then increases that
of the newly owned object; if both objects are the same, strange things

Boost list run by bdawes at, gregod at, cpdaniel at, john at