From ee0be23c9554c795c1b7c281f6fadc9c3f84106f Mon Sep 17 00:00:00 2001 From: Kolan Sh Date: Fri, 5 Oct 2012 17:15:55 +0400 Subject: [PATCH] Memory leak in C++ with shared_ptr example. --- cpp/mleak/mleak.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 cpp/mleak/mleak.cpp diff --git a/cpp/mleak/mleak.cpp b/cpp/mleak/mleak.cpp new file mode 100644 index 0000000..b66da12 --- /dev/null +++ b/cpp/mleak/mleak.cpp @@ -0,0 +1,28 @@ +#include + +class Foo { + public: + boost::shared_ptr s_ptr; + boost::weak_ptr w_ptr; + + Foo () + { + std::cout << "Foo (): allocate big piece by " << this << std::endl; + } + ~Foo () + { + std::cout << "~Foo (): deallocate the piece by " << this << std::endl; + } +}; + +int main (int argc, char *argv[]) +{ + boost::shared_ptr foo (new Foo); + + if (1 < argc ? argv[1][0] - '0' : 0) + foo->s_ptr = boost::shared_ptr (foo); + else + foo->w_ptr = boost::weak_ptr (foo); + + return 0; +}