Boost logo

Boost Users :

Subject: [Boost-users] shared_ptr leaking memory and crashing / gcc 3.4.6
From: Michael (boost_at_[hidden])
Date: 2008-10-17 18:44:14


Hello,

I figured that shared_ptr (boost 3.4, but I don't think this is really a
boost problem) will lose memory in the following example

#include <boost/shared_ptr.hpp>

struct X
{
   int y;
};
typedef boost::shared_ptr<X> XRef;

XRef getx()
{
   throw 0;
}

int main(int argc, char** argv)
{
   while (true)
   {
     XRef myX;
     try
     {
       myX = getx();
     }
     catch(...)
     {
     }
   }
}

Compile: g++ -I/usr/local/include test.C -o test

On every iteration of the while loop it loses memory and after approx. 1
million iterations it dies with signal 10 or 11.

Changing that pattern to
   try
   {
     XRef y = getx();
     myX = y;
   }
   catch(...)
   {
   }

fixes the problem (I assume in that case it will call shared_ptrs copy
constructor instead of its assignment operator).

Platform is FreeBSD 6, gcc 3.4.6. It only happens when compiled like
above (without optimization). On optimization levels 2 and above (-O2)
it works ok. Maybe somebody using gcc 3.4 can check if this also happens
on other platforms. gcc 4 works ok without any issues, so I assume this
is a compiler problem. It might still be a good idea to put a warning in
the documentation about this.

cheers
Michael


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net