shared_ptr memory leak in VC2005?
hi, guys, I found memory leak using the following code: #include <boost\array.hpp> #include <boost\shared_ptr.hpp> using namespace boost; class C { public: shared_ptr<C> pNext; }; void test() { shared_ptr<C> c0(new C()); shared_ptr<C> c1(new C()); c0 -> pNext = c1; c1 -> pNext = c0; } void main() { test(); _CrtDumpMemoryLeaks(); } when running with F5, it said: Detected memory leaks! Dumping objects -> d:\program files\microsoft visual studio 8\vc\include\crtdbg.h(1147) : {126} normal block at 0x003A64B0, 16 bytes long. Data: < E hd: > C4 17 45 00 01 00 00 00 01 00 00 00 68 64 3A 00 d:\program files\microsoft visual studio 8\vc\include\crtdbg.h(1147) : {125} normal block at 0x003A6468, 8 bytes long. Data: < c: d: > D0 63 3A 00 18 64 3A 00 d:\program files\microsoft visual studio 8\vc\include\crtdbg.h(1147) : {124} normal block at 0x003A6418, 16 bytes long. Data: < E c: > C4 17 45 00 01 00 00 00 01 00 00 00 D0 63 3A 00 d:\program files\microsoft visual studio 8\vc\include\crtdbg.h(1147) : {123} normal block at 0x003A63D0, 8 bytes long. Data: <hd: d: > 68 64 3A 00 B0 64 3A 00 Object dump complete. Any body have an idea about this? Thanks very much.
This isssue is well discussed in the shared_ptr docs. And it is also suggested there to use weak_ptr, instead of shared_ptr. shared_ptr can not resolve cyclic dependencies: Please read: http://www.boost.org/libs/smart_ptr/shared_ptr.htm ... "Because the implementation uses reference counting, cycles of *shared_ptr*instances will not be reclaimed. For example, if *main()* holds a *shared_ptr* to *A*, which directly or indirectly holds a * shared_ptr* back to *A*, *A*'s use count will be 2. Destruction of the original *shared_ptr* will leave *A* dangling with a use count of 1. Use weak_ptr <http://www.boost.org/libs/smart_ptr/weak_ptr.htm> to "break cycles."" ... With Kind Regards, Ovanes Markarian On 10/29/07, zzp good <baobeileaf@gmail.com> wrote:
hi, guys, I found memory leak using the following code:
#include <boost\array.hpp> #include <boost\shared_ptr.hpp> using namespace boost;
class C { public: shared_ptr<C> pNext; };
void test() { shared_ptr<C> c0(new C()); shared_ptr<C> c1(new C()); c0 -> pNext = c1; c1 -> pNext = c0; }
void main() { test();
_CrtDumpMemoryLeaks(); }
when running with F5, it said:
Detected memory leaks! Dumping objects -> d:\program files\microsoft visual studio 8\vc\include\crtdbg.h(1147) : {126} normal block at 0x003A64B0, 16 bytes long. Data: < E hd: > C4 17 45 00 01 00 00 00 01 00 00 00 68 64 3A 00 d:\program files\microsoft visual studio 8\vc\include\crtdbg.h(1147) : {125} normal block at 0x003A6468, 8 bytes long. Data: < c: d: > D0 63 3A 00 18 64 3A 00 d:\program files\microsoft visual studio 8\vc\include\crtdbg.h(1147) : {124} normal block at 0x003A6418, 16 bytes long. Data: < E c: > C4 17 45 00 01 00 00 00 01 00 00 00 D0 63 3A 00 d:\program files\microsoft visual studio 8\vc\include\crtdbg.h(1147) : {123} normal block at 0x003A63D0, 8 bytes long. Data: <hd: d: > 68 64 3A 00 B0 64 3A 00 Object dump complete.
Any body have an idea about this? Thanks very much.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
zzp good wrote:
hi, guys, I found memory leak using the following code:
#include <boost\array.hpp> #include <boost\shared_ptr.hpp> using namespace boost;
class C { public: shared_ptr<C> pNext; };
void test() { shared_ptr<C> c0(new C()); shared_ptr<C> c1(new C()); c0 -> pNext = c1; c1 -> pNext = c0; }
void main() { test();
_CrtDumpMemoryLeaks(); }
See http://www.boost.org/libs/smart_ptr/weak_ptr.htm. It's specifically designed to break these sorts of ownership cycles. Jeff Flinn
participants (3)
-
Jeff Flinn
-
Ovanes Markarian
-
zzp good