{"id":191018,"date":"2013-08-22T14:07:03","date_gmt":"2013-08-22T10:07:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=191018"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=191018","title":{"rendered":"<span class=\"post_title\">\u041f\u044f\u0442\u044c \u043f\u043e\u0434\u0432\u043e\u0434\u043d\u044b\u0445 \u043a\u0430\u043c\u043d\u0435\u0439 \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 shared_ptr<\/span>"},"content":{"rendered":"<div class=\"content html_format\"> \t\t\t\u041a\u043b\u0430\u0441\u0441 shared_ptr \u2014 \u044d\u0442\u043e \u0443\u0434\u043e\u0431\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u0435\u0442 \u0440\u0435\u0448\u0438\u0442\u044c \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430. \u041e\u0434\u043d\u0430\u043a\u043e \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u0441\u043e\u0432\u0435\u0440\u0448\u0430\u0442\u044c \u043e\u0448\u0438\u0431\u043e\u043a, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043e\u0442\u043b\u0438\u0447\u043d\u043e \u0437\u043d\u0430\u0442\u044c \u0435\u0433\u043e \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e. \u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u043c\u043e\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043b\u0435\u0437\u043d\u0430 \u0442\u0435\u043c, \u043a\u0442\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u044d\u0442\u0438\u043c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u043c.<\/p>\n<p>  \u042f \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c:  <\/p>\n<ul>\n<li>\u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u043f\u0435\u0440\u0435\u043a\u0440\u0435\u0441\u0442\u043d\u044b\u0435 \u0441\u0441\u044b\u043b\u043a\u0438;<\/li>\n<li>\u0447\u0435\u043c \u043e\u043f\u0430\u0441\u043d\u044b \u0431\u0435\u0437\u044b\u043c\u044f\u043d\u043d\u044b\u0435 shared_ptr;<\/li>\n<li>\u043a\u0430\u043a\u0438\u0435 \u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u0434\u0441\u0442\u0435\u0440\u0435\u0433\u0430\u044e\u0442 \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 shared_ptr \u0432 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0439 \u0441\u0440\u0435\u0434\u0435;<\/li>\n<li>\u043e \u0447\u0435\u043c \u0432\u0430\u0436\u043d\u043e \u043f\u043e\u043c\u043d\u0438\u0442\u044c, \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u044f \u0441\u0432\u043e\u044e \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u0443\u044e \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u044e\u0449\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f shared_ptr;<\/li>\n<li>\u043a\u0430\u043a\u0438\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0448\u0430\u0431\u043b\u043e\u043d\u0430 enable_shared_from_this.<\/li>\n<\/ul>\n<p>  <a name=\"habracut\"><\/a>\u041e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0438\u043c\u0435\u044e\u0442 \u043c\u0435\u0441\u0442\u043e \u043a\u0430\u043a \u0434\u043b\u044f boost::shared_ptr, \u0442\u0430\u043a \u0438 \u0434\u043b\u044f std::shared_ptr. \u0412 \u043a\u043e\u043d\u0446\u0435 \u0441\u0442\u0430\u0442\u044c\u0438 \u0432\u044b \u043d\u0430\u0439\u0434\u0435\u0442\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0441 \u043f\u043e\u043b\u043d\u044b\u043c\u0438 \u0442\u0435\u043a\u0441\u0442\u0430\u043c\u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c\u044b\u0445 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0435\u0439 (\u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 boost).<\/p>\n<h4>\u041f\u0435\u0440\u0435\u043a\u0440\u0435\u0441\u0442\u043d\u044b\u0435 \u0441\u0441\u044b\u043b\u043a\u0438<\/h4>\n<p>  \u0414\u0430\u043d\u043d\u0430\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e\u0439 \u0438 \u0441\u0432\u044f\u0437\u0430\u043d\u0430 \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c shared_ptr \u043e\u0441\u043d\u043e\u0432\u0430\u043d \u043d\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u0435 \u0441\u0441\u044b\u043b\u043e\u043a. \u0414\u043b\u044f \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u043e\u0431\u044a\u0435\u043a\u0442\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0432\u043b\u0430\u0434\u0435\u0435\u0442 shared_ptr, \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u0441\u0447\u0435\u0442\u0447\u0438\u043a. \u042d\u0442\u043e\u0442 \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0431\u0449\u0438\u043c \u0434\u043b\u044f \u0432\u0441\u0435\u0445 shared_ptr, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0445 \u043d\u0430 \u0434\u0430\u043d\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442.<\/p>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage2\/513\/c55\/301\/513c55301ff0b440423d8038925da6ae.png\"\/><\/p>\n<p>  \u041f\u0440\u0438 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u043d\u043e\u0432\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u043e\u0431\u044a\u0435\u043a\u0442 \u0441\u043e \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u043e\u043c \u0438 \u0432 \u043d\u0435\u0433\u043e \u043f\u043e\u043c\u0435\u0449\u0430\u0435\u0442\u0441\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 1. \u041f\u0440\u0438 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 1. \u041f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 \u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430 (\u0438\u043b\u0438 \u043f\u0440\u0438 \u0437\u0430\u043c\u0435\u043d\u0435 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f \u043f\u0443\u0442\u0435\u043c \u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u043d\u0438\u044f, \u0438\u043b\u0438 \u0432\u044b\u0437\u043e\u0432\u0430 reset), \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u0442\u0441\u044f \u043d\u0430 1.<\/p>\n<p>  \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440:  <\/p>\n<pre><code class=\"cpp\">struct Widget {     shared_ptr&lt;Widget&gt; otherWidget; };  void foo() {     shared_ptr&lt;Widget&gt; a(new Widget);     shared_ptr&lt;Widget&gt; b(new Widget);     a-&gt;otherWidget = b;     \/\/ \u0412 \u044d\u0442\u043e\u0439 \u0442\u043e\u0447\u043a\u0435 \u0443 \u0432\u0442\u043e\u0440\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0441\u0441\u044b\u043b\u043e\u043a = 2     b-&gt;otherWidget = a;     \/\/ \u0412 \u044d\u0442\u043e\u0439 \u0442\u043e\u0447\u043a\u0435 \u0443 \u043e\u0431\u043e\u0438\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0441\u0441\u044b\u043b\u043e\u043a = 2 } <\/code><\/pre>\n<p>  \u0427\u0442\u043e \u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0434\u0435\u0442 \u043f\u0440\u0438 \u0432\u044b\u0445\u043e\u0434\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 a \u0438 b \u0438\u0437 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f? \u0412 \u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0435 \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0442\u0441\u044f \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u043e\u0431\u044a\u0435\u043a\u0442\u044b. \u0423 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0431\u0443\u0434\u0435\u0442 \u0441\u0447\u0435\u0442\u0447\u0438\u043a = 1 (\u0432\u0435\u0434\u044c a \u0432\u0441\u0435 \u0435\u0449\u0435 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 b, \u0430 b \u2014 \u043d\u0430 a). \u041e\u0431\u044a\u0435\u043a\u0442\u044b \u201c\u0434\u0435\u0440\u0436\u0430\u0442\u201d \u0434\u0440\u0443\u0433 \u0434\u0440\u0443\u0433\u0430 \u0438 \u0443 \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043d\u0435\u0442 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a \u043d\u0438\u043c \u0434\u043e\u0441\u0442\u0443\u043f \u2014 \u044d\u0442\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u201c\u043f\u043e\u0442\u0435\u0440\u044f\u043d\u044b\u201d.<br \/>  \u0414\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 weak_ptr. \u041e\u0434\u043d\u0438\u043c \u0438\u0437 \u0442\u0438\u043f\u0438\u0447\u043d\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043f\u0435\u0440\u0435\u043a\u0440\u0435\u0441\u0442\u043d\u044b\u0445 \u0441\u0441\u044b\u043b\u043e\u043a \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u043b\u0443\u0447\u0430\u0439, \u043a\u043e\u0433\u0434\u0430 \u043e\u0434\u0438\u043d \u043e\u0431\u044a\u0435\u043a\u0442 \u0432\u043b\u0430\u0434\u0435\u0435\u0442 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0435\u0439 \u0434\u0440\u0443\u0433\u0438\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432<\/p>\n<pre><code class=\"cpp\">struct RootWidget {     list&lt;shared_ptr&lt;class Widget&gt; &gt; widgets; };  struct Widget {     shared_ptr&lt;class RootWidget&gt; parent; }; <\/code><\/pre>\n<p>  \u041f\u0440\u0438 \u0442\u0430\u043a\u043e\u043c \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0435 \u043a\u0430\u0436\u0434\u044b\u0439 Widget \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0435\u043f\u044f\u0442\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044e RootWidget \u0438 \u043d\u0430\u043e\u0431\u043e\u0440\u043e\u0442.<\/p>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage2\/8c0\/f47\/44b\/8c0f4744bc894b10994e21c57bf5d9a1.png\"\/><\/p>\n<p>  \u0412 \u0442\u0430\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0443\u0436\u043d\u043e \u043e\u0442\u0432\u0435\u0442\u0438\u0442\u044c \u043d\u0430 \u0432\u043e\u043f\u0440\u043e\u0441: \u201c\u041a\u0442\u043e \u043a\u0435\u043c \u0432\u043b\u0430\u0434\u0435\u0435\u0442?\u201d. \u041e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0438\u043c\u0435\u043d\u043d\u043e RootWidget \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0432\u043b\u0430\u0434\u0435\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u043c\u0438 Widget, \u0430 \u043d\u0435 \u043d\u0430\u043e\u0431\u043e\u0440\u043e\u0442. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440 \u043d\u0443\u0436\u043d\u043e \u0442\u0430\u043a:<\/p>\n<pre><code class=\"cpp\">struct Widget {     weak_ptr&lt;class RootWidget&gt; parent; }; <\/code><\/pre>\n<p>  \u0421\u043b\u0430\u0431\u044b\u0435 \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0435 \u043f\u0440\u0435\u043f\u044f\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044e \u043e\u0431\u044a\u0435\u043a\u0442\u0430. \u041e\u043d\u0438 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u044b \u0432 \u0441\u0438\u043b\u044c\u043d\u044b\u0435 \u0434\u0432\u0443\u043c\u044f \u0441\u043f\u043e\u0441\u043e\u0431\u0430\u043c\u0438:<\/p>\n<p>  1) \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 shared_ptr  <\/p>\n<pre><code class=\"cpp\">weak_ptr&lt;Widget&gt; w = \u2026; \/\/ \u0412 \u0441\u043b\u0443\u0447\u0430\u0435, \u0435\u0441\u043b\u0438 \u043e\u0431\u044a\u0435\u043a\u0442 \u0443\u0436\u0435 \u0443\u0434\u0430\u043b\u0435\u043d, \u0432 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0435 shared_ptr \u0431\u0443\u0434\u0435\u0442 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043e \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 shared_ptr&lt;Widget&gt; p( w ); <\/code><\/pre>\n<p>  2) \u041c\u0435\u0442\u043e\u0434 lock  <\/p>\n<pre><code class=\"cpp\">weak_ptr&lt;Widget&gt; w = \u2026; \/\/ \u0412 \u0441\u043b\u0443\u0447\u0430\u0435, \u0435\u0441\u043b\u0438 \u043e\u0431\u044a\u0435\u043a\u0442 \u0443\u0436\u0435 \u0443\u0434\u0430\u043b\u0435\u043d, \u0442\u043e p \u0431\u0443\u0434\u0435\u0442 \u043f\u0443\u0441\u0442\u044b\u043c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u043c  if( shared_ptr&lt;Widget&gt; p = w.lock() ) { \/\/ \u041e\u0431\u044a\u0435\u043a\u0442 \u043d\u0435 \u0431\u044b\u043b \u0443\u0434\u0430\u043b\u0435\u043d \u2013 \u0441 \u043d\u0438\u043c \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c } <\/code><\/pre>\n<p>  <b>\u0412\u044b\u0432\u043e\u0434:<\/b><br \/>  \u0412 \u0441\u043b\u0443\u0447\u0430\u0435 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u043e\u0432\u0435\u043d\u0438\u044f \u0432 \u043a\u043e\u0434\u0435 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u044b\u0445 \u0441\u0441\u044b\u043b\u043e\u043a, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 weak_ptr \u0434\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c.<\/p>\n<h4>\u0411\u0435\u0437\u044b\u043c\u044f\u043d\u043d\u044b\u0435 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438<\/h4>\n<p>  \u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0431\u0435\u0437\u044b\u043c\u044f\u043d\u043d\u044b\u0445 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u0439 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0441\u044f \u0432 \u0432\u043e\u043f\u0440\u043e\u0441\u0443 \u043e \u201c\u0442\u043e\u0447\u043a\u0430\u0445 \u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f\u201d (sequence points)  <\/p>\n<pre><code class=\"cpp\">\/\/ shared_ptr, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u044e foo - \u0431\u0435\u0437\u044b\u043c\u044f\u043d\u043d\u044b\u0439 foo( shared_ptr&lt;Widget&gt;(new Widget), bar() );  \/\/ shared_ptr, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u044e foo \u0438\u043c\u0435\u0435\u0442 \u0438\u043c\u044f p shared_ptr&lt;Widget&gt; p(new Widget); foo( p, bar() ); <\/code><\/pre>\n<p>  \u0418\u0437 \u044d\u0442\u0438\u0445 \u0434\u0432\u0443\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u0442 \u0432\u0441\u0435\u0433\u0434\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0442\u043e\u0440\u043e\u0439 \u2014 \u0434\u0430\u0432\u0430\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c \u0438\u043c\u0435\u043d\u0430. \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440, \u043a\u043e\u0433\u0434\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f bar \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0430 \u0432\u043e\u0442 \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"cpp\">int bar() {     throw std::runtime_error(\u201cException from bar()\u201d); } <\/code><\/pre>\n<p>  \u0414\u0435\u043b\u043e \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0432 \u043f\u0435\u0440\u0432\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043d\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d. \u0412\u0441\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0433\u043e \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430 \u0438 \u0444\u043b\u0430\u0433\u043e\u0432 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u044d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0442\u0438 \u0442\u0430\u043a:  <\/p>\n<ol>\n<li>new Widget<\/li>\n<li>\u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 bar<\/li>\n<li>\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 shared_ptr<\/li>\n<li>\u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 foo<\/li>\n<\/ol>\n<p>  \u041d\u0430\u0432\u0435\u0440\u043d\u044f\u043a\u0430 \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0443\u0432\u0435\u0440\u0435\u043d\u043d\u044b\u043c \u043b\u0438\u0448\u044c \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0432\u044b\u0437\u043e\u0432 foo \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u043c \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435\u043c, \u0430 shared_ptr \u0431\u0443\u0434\u0435\u0442 \u0441\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u0438\u0440\u043e\u0432\u0430\u043d \u043f\u043e\u0441\u043b\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430 (new Widget). \u041e\u0434\u043d\u0430\u043a\u043e \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0439 \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u0441\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u0438\u0440\u043e\u0432\u0430\u043d \u0441\u0440\u0430\u0437\u0443 \u043f\u043e\u0441\u043b\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430, \u043d\u0435\u0442.<\/p>\n<p>  \u0415\u0441\u043b\u0438 \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0432\u0442\u043e\u0440\u043e\u0433\u043e \u0448\u0430\u0433\u0430 \u0431\u0443\u0434\u0435\u0442 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043e \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 (\u0430 \u043e\u043d\u043e \u0432 \u043d\u0430\u0448\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043e \u0431\u0443\u0434\u0435\u0442), \u0442\u043e Widget \u0431\u0443\u0434\u0435\u0442 \u0441\u0447\u0438\u0442\u0430\u0442\u044c\u0441\u044f \u0441\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c, \u043d\u043e shared_ptr \u0435\u0449\u0435 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0438\u043c \u0432\u043b\u0430\u0434\u0435\u0442\u044c. \u0412 \u0438\u0442\u043e\u0433\u0435 \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u044d\u0442\u043e\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u0442\u0435\u0440\u044f\u043d\u0430. \u042f \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043b \u0434\u0430\u043d\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u043d\u0430 gcc 4.7.2. \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u0432\u044b\u0437\u043e\u0432\u0430 \u0431\u044b\u043b \u0442\u0430\u043a\u0438\u043c, \u0447\u0442\u043e shared_ptr+new \u0432\u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u043e\u043f\u0446\u0438\u0439 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u043d\u0435 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u043b\u0438\u0441\u044c \u0432\u044b\u0437\u043e\u0432\u043e\u043c bar. \u041d\u043e \u043f\u043e\u043b\u0430\u0433\u0430\u0442\u044c\u0441\u044f \u0438\u043c\u0435\u043d\u043d\u043e \u043d\u0430 \u0442\u0430\u043a\u043e\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u043d\u0435 \u0441\u0442\u043e\u0438\u0442 \u2013 \u044d\u0442\u043e \u043d\u0435 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043e. \u0411\u0443\u0434\u0443 \u043f\u0440\u0438\u0437\u043d\u0430\u0442\u0435\u043b\u0435\u043d, \u0435\u0441\u043b\u0438 \u043c\u043d\u0435 \u043f\u043e\u0434\u0441\u043a\u0430\u0436\u0443\u0442 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440, \u0435\u0433\u043e \u0432\u0435\u0440\u0441\u0438\u044e \u0438 \u043e\u043f\u0446\u0438\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438, \u0434\u043b\u044f \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0439 \u043a\u043e\u0434 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u0442 \u043a \u043e\u0448\u0438\u0431\u043a\u0435.<\/p>\n<p>  <b>\u0412\u044b\u0432\u043e\u0434:<\/b><br \/>  \u0414\u0430\u0432\u0430\u0439\u0442\u0435 shared_ptr \u0438\u043c\u0435\u043d\u0430, \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u043a\u043e\u0434 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442 \u044d\u0442\u043e\u0433\u043e \u043c\u0435\u043d\u0435\u0435 \u043b\u0430\u043a\u043e\u043d\u0438\u0447\u043d\u044b\u043c.<\/p>\n<h4>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u0430\u0445<\/h4>\n<p>  \u041f\u043e\u0434\u0441\u0447\u0435\u0442 \u0441\u0441\u044b\u043b\u043e\u043a \u0432 shared_ptr \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u043e\u0433\u043e \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430. \u041c\u044b \u0431\u0435\u0437 \u043e\u043f\u0430\u0441\u043a\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438 \u043d\u0430 \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u043e\u0431\u044a\u0435\u043a\u0442 \u0438\u0437 \u0440\u0430\u0437\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432. \u0412\u043e \u0432\u0441\u044f\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u043c\u044b \u043d\u0435 \u043f\u0440\u0438\u0432\u044b\u043a\u043b\u0438 \u0431\u0435\u0441\u043f\u043e\u043a\u043e\u0438\u0442\u044c\u0441\u044f \u043e \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u0435 \u0441\u0441\u044b\u043b\u043e\u043a (\u043f\u043e\u0442\u043e\u043a\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c \u0441\u0430\u043c\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u2013 \u0434\u0440\u0443\u0433\u0430\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430).<\/p>\n<p>  \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0439 shared_ptr:  <\/p>\n<pre><code class=\"cpp\">shared_ptr&lt;Widget&gt; globalSharedPtr(new Widget);  void read() {     shared_ptr&lt;Widget&gt; x = globalSharedPtr;     \/\/ \u0421\u0434\u0435\u043b\u0430\u0442\u044c \u0447\u0442\u043e-\u043d\u0438\u0431\u0443\u0434\u044c \u0441 Widget } <\/code><\/pre>\n<p>  \u0417\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u0435 \u0432\u044b\u0437\u043e\u0432 read \u0438\u0437 \u0440\u0430\u0437\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0438 \u0432\u044b \u0443\u0432\u0438\u0434\u0438\u0442\u0435, \u0447\u0442\u043e \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u0432 \u043a\u043e\u0434\u0435 \u043d\u0435 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 (\u0434\u043e \u0442\u0435\u0445 \u043f\u043e\u0440, \u043f\u043e\u043a\u0430 \u0432\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0435 \u043d\u0430\u0434 Widget \u043f\u043e\u0442\u043e\u043a\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0435 \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438).<\/p>\n<p>  \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0435\u0441\u0442\u044c \u0435\u0449\u0435 \u043e\u0434\u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f:  <\/p>\n<pre><code class=\"cpp\">void write() {      globalSharedPtr.reset( new Widget ); } <\/code><\/pre>\n<p>  \u0423\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e shared_ptr \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0441\u043b\u043e\u0436\u043d\u043e, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f \u043f\u0440\u0438\u0432\u0435\u0434\u0443 \u043a\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u0445\u0435\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443. \u0420\u0430\u0437\u0443\u043c\u0435\u0435\u0442\u0441\u044f, \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0438\u0439 \u043a\u043e\u0434 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0438\u043d\u0430\u0447\u0435.  <\/p>\n<pre><code class=\"cpp\">shared_ptr::shared_ptr(const shared_ptr&lt;T&gt;& x) { A1:    pointer = x.pointer; A2:    counter = x.counter; A3:    atomic_increment( *counter ); }  shared_ptr&lt;T&gt;::reset(T* newObject) { B1:    if( atomic_decrement( *counter ) == 0 ) { B2:        delete pointer; B3:        delete counter; B4:    } B5:    pointer = newObject; B6:    counter = new Counter; } <\/code><\/pre>\n<p>  \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u043f\u0435\u0440\u0432\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u043d\u0430\u0447\u0430\u043b \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c globalSharedPtr (read), \u0430 \u0432\u0442\u043e\u0440\u043e\u0439 \u043f\u043e\u0442\u043e\u043a \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 reset \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0436\u0435 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f (write). \u0412 \u0438\u0442\u043e\u0433\u0435 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:  <\/p>\n<ol>\n<li>\u041f\u043e\u0442\u043e\u043a1 \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0442\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043b \u0441\u0442\u0440\u043e\u043a\u0443 A2, \u043d\u043e \u0435\u0449\u0435 \u043d\u0435 \u043f\u0435\u0440\u0435\u0448\u0435\u043b \u043a \u0441\u0442\u0440\u043e\u043a\u0435 A3 (\u0430\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u0439 \u0438\u043d\u043a\u0440\u0435\u043c\u0435\u043d\u0442). <\/li>\n<li>\u041f\u043e\u0442\u043e\u043a2 \u0432 \u044d\u0442\u043e \u0432\u0440\u0435\u043c\u044f \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u043b \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u043d\u0430 \u0441\u0442\u0440\u043e\u043a\u0435 B1, \u0443\u0432\u0438\u0434\u0435\u043b, \u0447\u0442\u043e \u043f\u043e\u0441\u043b\u0435 \u0443\u043c\u0435\u043d\u044c\u0448\u0435\u043d\u0438\u044f \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0441\u0442\u0430\u043b \u0440\u0430\u0432\u0435\u043d \u043d\u0443\u043b\u044e \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043b \u0441\u0442\u0440\u043e\u043a\u0438 B2 \u0438 B3. <\/li>\n<li>\u041f\u043e\u0442\u043e\u043a1 \u0434\u043e\u0445\u043e\u0434\u0438\u0442 \u0434\u043e \u0441\u0442\u0440\u043e\u043a\u0438 A3 \u0438 \u043f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u043e \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u0441\u0447\u0435\u0442\u0447\u0438\u043a, \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0443\u0436\u0435 \u043d\u0435\u0442. <\/li>\n<\/ol>\n<p>  \u0410 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0438 \u0442\u0430\u043a, \u0447\u0442\u043e \u043f\u043e\u0442\u043e\u043a1 \u043d\u0430 \u0441\u0442\u0440\u043e\u043a\u0435 A2 \u0443\u0441\u043f\u0435\u0435\u0442 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0434\u043e \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u043f\u043e\u0442\u043e\u043a2 \u0432\u044b\u0437\u043e\u0432\u0435\u0442 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432, \u043d\u043e \u043f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e \u043a\u0430\u043a \u043f\u043e\u0442\u043e\u043a2 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u043b \u0443\u043c\u0435\u043d\u044c\u0448\u0435\u043d\u0438\u0435 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430. \u0422\u043e\u0433\u0434\u0430 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043d\u043e\u0432\u044b\u0439 shared_ptr, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u044b\u0439 \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0438 \u043e\u0431\u044a\u0435\u043a\u0442.<\/p>\n<p>  \u041c\u043e\u0436\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0439 \u043a\u043e\u0434:  <\/p>\n<pre><code class=\"cpp\">shared_ptr&lt;Widget&gt; globalSharedPtr(new Widget); mutex_t globalSharedPtrMutex;  void resetGlobal(Widget* x) {     write_lock_t l(globalSharedPtrMutex);     globalSharedPtr.reset( x ); }  shared_ptr&lt;Widget&gt; getGlobal() {     read_lock_t l(globalSharedPtrMutex);     return globalSharedPtr; }  void read() {     shared_ptr&lt;Widget&gt; x = getGlobal();     \/\/ \u0412\u043e\u0442 \u0441 \u044d\u0442\u0438\u043c x \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c }  void write() {      resetGlobal( new Widget ); } <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0442\u0430\u043a\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043c\u043e\u0436\u043d\u043e \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u044d\u0442\u0438\u043c shared_ptr. <\/p>\n<p>  <b>\u0412\u044b\u0432\u043e\u0434:<\/b> \u0435\u0441\u043b\u0438 \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 shared_ptr \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u0440\u0430\u0437\u043d\u044b\u043c \u043f\u043e\u0442\u043e\u043a\u0430\u043c \u0438 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u043d, \u0442\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u0437\u0430\u0431\u043e\u0442\u0438\u0442\u044c\u0441\u044f \u043e \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u044d\u0442\u043e\u043c\u0443 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0443 shared_ptr.<\/p>\n<h4>\u041e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0440\u0430\u0437\u0440\u0443\u0448\u0435\u043d\u0438\u044f \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u044e\u0449\u0435\u0433\u043e \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u0430 \u0434\u043b\u044f shared_ptr<\/h4>\n<p>  \u0414\u0430\u043d\u043d\u0430\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u043c\u043e\u0436\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u043c\u0435\u0441\u0442\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0435\u0441\u043b\u0438 \u0432\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u044e\u0449\u0438\u0439 \u0444\u0443\u043d\u043a\u0442\u043e\u0440 \u0432 \u0441\u043e\u0447\u0435\u0442\u0430\u043d\u0438\u0438 \u0441\u043e \u0441\u043b\u0430\u0431\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 (weak_ptr). \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u043e\u0437\u0434\u0430\u0442\u044c shared_ptr \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0434\u0440\u0443\u0433\u043e\u0433\u043e shared_ptr, \u0434\u043e\u0431\u0430\u0432\u0438\u0432 \u043d\u043e\u0432\u043e\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u043f\u0435\u0440\u0435\u0434 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435\u043c (\u043f\u043e \u0441\u0443\u0442\u0438 \u0448\u0430\u0431\u043b\u043e\u043d \u201c\u0424\u0430\u0441\u0430\u0434\u201d). \u0422\u0430\u043a \u0432\u044b \u043c\u043e\u0433\u043b\u0438 \u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0431\u0430\u0437\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0445, \u0438\u0437\u044a\u044f\u0432 \u0435\u0433\u043e \u0438\u0437 \u043f\u0443\u043b\u0430 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0439, \u0430 \u043f\u043e \u043e\u043a\u043e\u043d\u0447\u0430\u043d\u0438\u0438 \u0440\u0430\u0431\u043e\u0442\u044b \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0441 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u043c \u2014 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0432 \u043f\u0443\u043b.  <\/p>\n<pre><code class=\"cpp\">typedef shared_ptr&lt;Connection&gt; ptr_t;  class ConnectionReleaser {     list&lt;ptr_t&gt;& whereToReturn;     ptr_t connectionToRelease; public:     ConnectionReleaser(list&lt;ptr_t&gt;& lst, const ptr_t& x):whereToReturn(lst), connectionToRelease(x) {}      void operator()(Connection*) {         whereToReturn.push_back( connectionToRelease ); \/\/ \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0441\u0442\u0440\u043e\u0447\u043a\u0443         connectionToRelease.reset();     } };  ptr_t getConnection() {     ptr_t c( connectionList.back() );     connectionList.pop_back();     ptr_t r( c.get(), ConnectionReleaser( connectionList, c ) );     return r;  } <\/code><\/pre>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage2\/381\/c9e\/4d3\/381c9e4d3a46896aa1e970274f790d82.png\"\/><\/p>\n<p>  \u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043e\u0431\u044a\u0435\u043a\u0442, \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u044b\u0439 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u044e\u0449\u0435\u0433\u043e \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u0430 \u0434\u043b\u044f shared_ptr, \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0437\u0440\u0443\u0448\u0435\u043d \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u043e\u0433\u0434\u0430, \u043a\u043e\u0433\u0434\u0430 \u0432\u0441\u0435 \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u043e\u0431\u044a\u0435\u043a\u0442 \u0431\u0443\u0434\u0443\u0442 \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0435\u043d\u044b \u2014 \u043a\u0430\u043a \u0441\u0438\u043b\u044c\u043d\u044b\u0435(shared_ptr), \u0442\u0430\u043a \u0438 \u0441\u043b\u0430\u0431\u044b\u0435(weak_ptr). \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0435\u0441\u043b\u0438 ConnectionReleaser \u043d\u0435 \u043f\u043e\u0437\u0430\u0431\u043e\u0442\u0438\u0442\u0441\u044f \u043e \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u201c\u043e\u0442\u043f\u0443\u0441\u0442\u0438\u0442\u044c\u201d \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u044b\u0439 \u0435\u043c\u0443 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c (connectionToRelease), \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0441\u0438\u043b\u044c\u043d\u0443\u044e \u0441\u0441\u044b\u043b\u043a\u0443, \u043f\u043e\u043a\u0430 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0445\u043e\u0442\u044f \u0431\u044b \u043e\u0434\u0438\u043d weak_ptr \u043e\u0442 shared_ptr, \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 getConnection. \u042d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043d\u0435\u043f\u0440\u0438\u044f\u0442\u043d\u043e\u043c\u0443 \u0438 \u043d\u0435\u043e\u0436\u0438\u0434\u0430\u043d\u043d\u043e\u043c\u0443 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044e \u0432\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f.<\/p>\n<p>  \u0412\u043e\u0437\u043c\u043e\u0436\u0435\u043d \u0442\u0430\u043a \u0436\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442, \u043a\u043e\u0433\u0434\u0430 \u0432\u044b \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435\u0441\u044c bind \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u044e\u0449\u0435\u0433\u043e \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u0430. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"cpp\">void releaseConnection(std::list&lt;ptr_t&gt;& whereToReturn, ptr_t& connectionToRelease) {     whereToReturn.push_back( connectionToRelease );     \/\/ \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0441\u0442\u0440\u043e\u0447\u043a\u0443     connectionToRelease.reset(); }  ptr_t getConnection() {     ptr_t c( connectionList.back() );     connectionList.pop_back();     ptr_t r( c.get(), boost::bind(&releaseConnection, boost::ref(connectionList), c) );     return r; } <\/code><\/pre>\n<p>  \u041f\u043e\u043c\u043d\u0438\u0442\u0435, \u0447\u0442\u043e bind \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u044b\u0435 \u0435\u043c\u0443 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b (\u043a\u0440\u043e\u043c\u0435 \u0441\u043b\u0443\u0447\u0430\u044f \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c boost::ref), \u0438 \u0435\u0441\u043b\u0438 \u0441\u0440\u0435\u0434\u0438 \u043d\u0438\u0445 \u0431\u0443\u0434\u0435\u0442 shared_ptr, \u0442\u043e \u0435\u0433\u043e \u0442\u043e\u0436\u0435 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043e\u0447\u0438\u0441\u0442\u0438\u0442\u044c, \u0434\u0430\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0443\u0436\u0435 \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b.<\/p>\n<p>  <b>\u0412\u044b\u0432\u043e\u0434:<\/b> \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0435 \u0432 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u044e\u0449\u0435\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432\u0441\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u043e\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u0440\u0438 \u0440\u0430\u0437\u0440\u0443\u0448\u0435\u043d\u0438\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u0441\u0438\u043b\u044c\u043d\u043e\u0439 \u0441\u0441\u044b\u043b\u043a\u0438. \u0421\u0431\u0440\u043e\u0441\u044c\u0442\u0435 \u0432\u0441\u0435 shared_ptr, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u043f\u0440\u0438\u0447\u0438\u043d\u0435 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0447\u043b\u0435\u043d\u0430\u043c \u0432\u0430\u0448\u0435\u0433\u043e \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u0430. \u0415\u0441\u043b\u0438 \u0432\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 bind, \u0442\u043e \u043d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0439\u0442\u0435, \u0447\u0442\u043e \u043e\u043d \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u044b\u0435 \u0435\u043c\u0443 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b.<\/p>\n<h4>\u041e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0448\u0430\u0431\u043b\u043e\u043d\u043e\u043c enable_shared_from_this<\/h4>\n<p>  \u0418\u043d\u043e\u0433\u0434\u0430 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c shared_ptr \u0438\u0437 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0441\u0430\u043c\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430. \u041f\u043e\u043f\u044b\u0442\u043a\u0430 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043d\u043e\u0432\u043e\u0433\u043e shared_ptr \u043e\u0442 this \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u0442 \u043a \u043d\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u043c\u0443 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044e (\u0441\u043a\u043e\u0440\u0435\u0435 \u0432\u0441\u0435\u0433\u043e \u043a \u0430\u0432\u0430\u0440\u0438\u0439\u043d\u043e\u043c\u0443 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b), \u0432 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442 intrusive_ptr, \u0434\u043b\u044f \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u044d\u0442\u043e \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0431\u044b\u0447\u043d\u043e\u0439 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u043e\u0439. \u0414\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0431\u044b\u043b \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u043d \u0448\u0430\u0431\u043b\u043e\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441-\u043f\u0440\u0438\u043c\u0435\u0441\u044c enable_shared_from_this.<\/p>\n<p>  \u0428\u0430\u0431\u043b\u043e\u043d enable_shared_from_this \u0443\u0441\u0442\u0440\u043e\u0435\u043d \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c: \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u043b\u0430\u0441\u0441\u0430 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442\u0441\u044f weak_ptr, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0438 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 shared_ptr \u043f\u043e\u043c\u0435\u0449\u0430\u0435\u0442\u0441\u044f \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u044d\u0442\u043e\u0442 \u0441\u0430\u043c\u044b\u0439 shared_ptr. \u041f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 \u043c\u0435\u0442\u043e\u0434\u0430 shared_from_this \u043e\u0431\u044a\u0435\u043a\u0442\u0430, weak_ptr \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442\u0441\u044f \u0432 shared_ptr \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440. \u0421\u0445\u0435\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0448\u0430\u0431\u043b\u043e\u043d \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"cpp\">template&lt;class T&gt;  class enable_shared_from_this {     weak_ptr&lt;T&gt; weak_this_; public:     shared_ptr&lt;T&gt; shared_from_this() {         \/\/ \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u043b\u0430\u0431\u043e\u0439 \u0441\u0441\u044b\u043b\u043a\u0438 \u0432 \u0441\u0438\u043b\u044c\u043d\u0443\u044e \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 shared_ptr         shared_ptr&lt;T&gt; p( weak_this_ );         return p;     } };  class Widget: public enable_shared_from_this&lt;Widget&gt; {}; <\/code><\/pre>\n<p>  \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 shared_ptr \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0441\u043b\u0443\u0447\u0430\u044f \u0441\u0445\u0435\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"cpp\">shared_ptr::shared_ptr(T* object) {     pointer = object;     counter = new Counter;     object-&gt;weak_this_ = *this; } <\/code><\/pre>\n<p>  \u0412\u0430\u0436\u043d\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c, \u0447\u0442\u043e \u043f\u0440\u0438 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 weak_this_ \u0435\u0449\u0435 \u043d\u0438 \u043d\u0430 \u0447\u0442\u043e \u043d\u0435 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442. \u041f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0430\u044f \u0441\u0441\u044b\u043b\u043a\u0430 \u0432 \u043d\u0435\u043c \u043f\u043e\u044f\u0432\u0438\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u0441\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u0431\u0443\u0434\u0435\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u043d \u0432 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 shared_ptr. \u041b\u044e\u0431\u0430\u044f \u043f\u043e\u043f\u044b\u0442\u043a\u0430 \u0432\u044b\u0437\u043e\u0432\u0430 shared_from_this \u0438\u0437 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u0442 \u043a bad_weak_ptr \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044e.  <\/p>\n<pre><code class=\"cpp\">struct BadWidget: public enable_shared_from_this&lt;BadWidget&gt; {     BadWidget() {         \/\/ \u041f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 shared_from_this() \u0431\u0443\u0434\u0435\u0442 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043e bad_weak_ptr          cout &lt;&lt; shared_from_this() &lt;&lt; endl;     } }; <\/code><\/pre>\n<p>  \u041a \u0442\u0430\u043a\u0438\u043c \u0436\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u044f\u043c \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u0442 \u043f\u043e\u043f\u044b\u0442\u043a\u0430 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c\u0441\u044f \u043a shared_from_this \u0438\u0437 \u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430, \u043d\u043e \u0443\u0436\u0435 \u043f\u043e \u0434\u0440\u0443\u0433\u043e\u0439 \u043f\u0440\u0438\u0447\u0438\u043d\u0435: \u0432 \u043c\u043e\u043c\u0435\u043d\u0442 \u0440\u0430\u0437\u0440\u0443\u0448\u0435\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0443\u0436\u0435 \u0441\u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u043d\u0430 \u043d\u0435\u0433\u043e \u043d\u0435 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u0441\u0438\u043b\u044c\u043d\u044b\u0445 \u0441\u0441\u044b\u043b\u043e\u043a (\u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0434\u0435\u043a\u0440\u0435\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d).  <\/p>\n<pre><code class=\"cpp\">struct BadWidget: public enable_shared_from_this&lt;BadWidget&gt; {     ~BadWidget() {         \/\/ \u041f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 shared_from_this() \u0431\u0443\u0434\u0435\u0442 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043e bad_weak_ptr         cout &lt;&lt; shared_from_this() &lt;&lt; endl;     } }; <\/code><\/pre>\n<p>  \u0421\u043e \u0432\u0442\u043e\u0440\u044b\u043c \u0441\u043b\u0443\u0447\u0430\u0435\u043c (\u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440) \u043c\u0430\u043b\u043e \u0447\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u0442\u044c. \u0415\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u2014 \u043f\u043e\u0437\u0430\u0431\u043e\u0442\u0438\u0442\u044c\u0441\u044f \u043e \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c shared_from_this \u0438 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u044d\u0442\u043e\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u043b\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440.<\/p>\n<p>  \u0421 \u043f\u0435\u0440\u0432\u044b\u043c \u0441\u043b\u0443\u0447\u0430\u0435\u043c \u0432\u0441\u0435 \u043e\u0431\u0441\u0442\u043e\u0438\u0442 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0449\u0435. \u041d\u0430\u0432\u0435\u0440\u043d\u044f\u043a\u0430 \u0432\u044b \u0443\u0436\u0435 \u0440\u0435\u0448\u0438\u043b\u0438, \u0447\u0442\u043e \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0434\u043b\u044f \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u0438\u044f \u0432\u0430\u0448\u0435\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u2014 shared_ptr, \u0442\u043e\u0433\u0434\u0430 \u0431\u0443\u0434\u0435\u0442 \u0443\u043c\u0435\u0441\u0442\u043d\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0432 \u0437\u0430\u043a\u0440\u044b\u0442\u0443\u044e \u0447\u0430\u0441\u0442\u044c \u043a\u043b\u0430\u0441\u0441\u0430 \u0438 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f shared_ptr \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u0432\u0430\u043c \u0442\u0438\u043f\u0430. \u0415\u0441\u043b\u0438 \u043f\u0440\u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f shared_from_this, \u0442\u043e \u0434\u043b\u044f \u044d\u0442\u043e\u0439 \u0446\u0435\u043b\u0438 \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u043b\u043e\u0433\u0438\u043a\u0443 \u0432 \u043c\u0435\u0442\u043e\u0434 init.<\/p>\n<pre><code class=\"cpp\">class GoodWidget: public enable_shared_from_this&lt;GoodWidget&gt; {         void init() {                 cout &lt;&lt; shared_from_this() &lt;&lt; endl;         } public:         static shared_ptr&lt;GoodWidget&gt; create() {                 shared_ptr&lt;GoodWidget&gt; p(new GoodWidget);                 p-&gt;init();                 return p;         } }; <\/code><\/pre>\n<p>  <b>\u0412\u044b\u0432\u043e\u0434:<\/b><br \/>  \u0418\u0437\u0431\u0435\u0433\u0430\u0439\u0442\u0435 \u0432\u044b\u0437\u043e\u0432\u043e\u0432 (\u043f\u0440\u044f\u043c\u044b\u0445 \u0438\u043b\u0438 \u043a\u043e\u0441\u0432\u0435\u043d\u043d\u044b\u0445) shared_from_this \u0438\u0437 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u043e\u0432 \u0438 \u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u043e\u0432. \u0412 \u0441\u043b\u0443\u0447\u0430\u0435, \u0435\u0441\u043b\u0438 \u0434\u043b\u044f \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0439 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0434\u043e\u0441\u0442\u0443\u043f \u043a shared_from_this: \u0441\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u043c\u0435\u0442\u043e\u0434 init, \u0434\u0435\u043b\u0435\u0433\u0438\u0440\u0443\u0439\u0442\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u043c\u0443 \u043c\u0435\u0442\u043e\u0434\u0443 \u0438 \u0441\u0434\u0435\u043b\u0430\u0439\u0442\u0435 \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u044d\u0442\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430.<\/p>\n<h4>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h4>\n<p>  \u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b 5 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0435\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f shared_ptr \u0438 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u044b \u043e\u0431\u0449\u0438\u0435 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0430\u0446\u0438\u0438 \u043f\u043e \u0438\u0437\u0431\u0435\u0436\u0430\u043d\u0438\u044e \u043f\u043e\u0442\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u0440\u043e\u0431\u043b\u0435\u043c.<\/p>\n<p>  \u0425\u043e\u0442\u044f shared_ptr \u0438 \u0441\u043d\u0438\u043c\u0430\u0435\u0442 \u0441 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u043f\u0440\u043e\u0431\u043b\u0435\u043c, \u0437\u043d\u0430\u043d\u0438\u0435 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0435\u0433\u043e \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430 (\u0445\u043e\u0442\u044c \u0438 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e) \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u0434\u043b\u044f \u0433\u0440\u0430\u043c\u043e\u0442\u043d\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f shared_ptr. \u042f \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u0432\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0438\u0437\u0443\u0447\u0438\u0442\u044c \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e shared_ptr, \u0430 \u0442\u0430\u043a \u0436\u0435 \u043a\u043b\u0430\u0441\u0441\u043e\u0432, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0441 \u043d\u0438\u043c. \u0421\u043e\u0431\u043b\u044e\u0434\u0435\u043d\u0438\u0435 \u0440\u044f\u0434\u0430 \u043f\u0440\u043e\u0441\u0442\u044b\u0445 \u043f\u0440\u0430\u0432\u0438\u043b \u043c\u043e\u0436\u0435\u0442 \u0443\u0431\u0435\u0440\u0435\u0447\u044c \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u043e\u0442 \u043d\u0435\u0436\u0435\u043b\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u043f\u0440\u043e\u0431\u043b\u0435\u043c.<\/p>\n<h4>\u041b\u0438\u0442\u0435\u0440\u0430\u0442\u0443\u0440\u0430<\/h4>\n<p>  <\/p>\n<ul>\n<li><a href=\"http:\/\/www.boost.org\/doc\/libs\/1_54_0\/libs\/smart_ptr\/shared_ptr.htm\">\u0414\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f boost.org<\/a><\/li>\n<li>Scott Meyers <i>\u00abMore Effective C++: 35 New Ways to Improve Your Programs and Designs\u00bb<\/i><\/li>\n<\/ul>\n<h4>\u041f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435<\/h4>\n<p>  \u0412 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u044b \u043f\u043e\u043b\u043d\u044b\u0435 \u0442\u0435\u043a\u0441\u0442\u044b \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c \u0434\u043b\u044f \u0438\u043b\u043b\u044e\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0445 \u0432 \u0441\u0442\u0430\u0442\u044c\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0414\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u044b\u0445 \u0441\u0441\u044b\u043b\u043e\u043a<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">#include &lt;string&gt; #include &lt;iostream&gt; #include &lt;boost\/shared_ptr.hpp&gt; #include &lt;boost\/weak_ptr.hpp&gt;  class BadWidget { \tstd::string name; \tboost::shared_ptr&lt;BadWidget&gt; otherWidget; public: \tBadWidget(const std::string& n):name(n) { \t\tstd::cout &lt;&lt; &quot;BadWidget &quot; &lt;&lt; name &lt;&lt; std::endl; \t}  \t~BadWidget() { \t\tstd::cout &lt;&lt; &quot;~BadWidget &quot; &lt;&lt; name &lt;&lt; std::endl; \t}  \tvoid setOther(const boost::shared_ptr&lt;BadWidget&gt;& x) { \t\totherWidget = x; \t\tstd::cout &lt;&lt; name &lt;&lt; &quot; now points to &quot; &lt;&lt; x-&gt;name &lt;&lt; std::endl; \t} };  class GoodWidget { \tstd::string name; \tboost::weak_ptr&lt;GoodWidget&gt; otherWidget; public: \tGoodWidget(const std::string& n):name(n) { \t\tstd::cout &lt;&lt; &quot;GoodWidget &quot; &lt;&lt; name &lt;&lt; std::endl; \t}  \t~GoodWidget() { \t\tstd::cout &lt;&lt; &quot;~GoodWidget &quot; &lt;&lt; name &lt;&lt; std::endl; \t}  \tvoid setOther(const boost::shared_ptr&lt;GoodWidget&gt;& x) { \t\totherWidget = x; \t\tstd::cout &lt;&lt; name &lt;&lt; &quot; now points to &quot; &lt;&lt; x-&gt;name &lt;&lt; std::endl; \t} };  int main() { \t{ \/\/ \u0412 \u044d\u0442\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0443\u0442\u0435\u0447\u043a\u0430 \u043f\u0430\u043c\u044f\u0442\u0438 \t\tstd::cout &lt;&lt; &quot;====== Example 3&quot; &lt;&lt; std::endl; \t\tboost::shared_ptr&lt;BadWidget&gt; w1(new BadWidget(&quot;3_First&quot;)); \t\tboost::shared_ptr&lt;BadWidget&gt; w2(new BadWidget(&quot;3_Second&quot;)); \t\tw1-&gt;setOther( w2 ); \t\tw2-&gt;setOther( w1 ); \t} \t{ \/\/ \u0410 \u0432 \u044d\u0442\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u044b\u0439 weak_ptr \u0438 \u0443\u0442\u0435\u0447\u043a\u0438 \u043f\u0430\u043c\u044f\u0442\u0438 \u043d\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \t\tstd::cout &lt;&lt; &quot;====== Example 3&quot; &lt;&lt; std::endl; \t\tboost::shared_ptr&lt;GoodWidget&gt; w1(new GoodWidget(&quot;4_First&quot;)); \t\tboost::shared_ptr&lt;GoodWidget&gt; w2(new GoodWidget(&quot;4_Second&quot;)); \t\tw1-&gt;setOther( w2 ); \t\tw2-&gt;setOther( w1 ); \t} \treturn 0; } <\/code><\/pre>\n<\/div>\n<\/div>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0414\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f weak_ptr \u0432 shared_ptr<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">#include &lt;iostream&gt; #include &lt;boost\/shared_ptr.hpp&gt; #include &lt;boost\/weak_ptr.hpp&gt;  class Widget {};  int main() { \tboost::weak_ptr&lt;Widget&gt; w; \t\/\/ \u0412 \u044d\u0442\u043e\u0439 \u0442\u043e\u0447\u043a\u0435 weak_ptr \u043d\u0438 \u043d\u0430 \u0447\u0442\u043e \u043d\u0435 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \t\/\/ \u041c\u0435\u0442\u043e\u0434 lock \u0432\u0435\u0440\u043d\u0435\u0442 \u043f\u0443\u0441\u0442\u043e\u0439 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \tstd::cout &lt;&lt; __LINE__ &lt;&lt; &quot;: &quot; &lt;&lt; w.lock().get() &lt;&lt; std::endl; \t\/\/ \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 shared_ptr \u043e\u0442 \u044d\u0442\u043e\u0433\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u0442 \u043a \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044e \ttry { \t\tboost::shared_ptr&lt;Widget&gt; tmp ( w ); \t} catch (const boost::bad_weak_ptr&) { \t\tstd::cout &lt;&lt; __LINE__ &lt;&lt; &quot;: bad_weak_ptr&quot; &lt;&lt; std::endl; \t}  \tboost::shared_ptr&lt;Widget&gt; p(new Widget); \t\/\/ \u0422\u0435\u043f\u0435\u0440\u044c \u0443 weak_ptr \u0435\u0441\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \tw = p;  \t\/\/ \u041c\u0435\u0442\u043e\u0434 lock \u0432\u0435\u0440\u043d\u0435\u0442 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \tstd::cout &lt;&lt; __LINE__ &lt;&lt; &quot;: &quot; &lt;&lt; w.lock().get() &lt;&lt; std::endl; \t\/\/ \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 shared_ptr \u043e\u0442 \u044d\u0442\u043e\u0433\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f \u0442\u043e\u0436\u0435 \u0432\u0435\u0440\u043d\u0435\u0442 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c. \u0418\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \tstd::cout &lt;&lt; __LINE__ &lt;&lt; &quot;: &quot; &lt;&lt; boost::shared_ptr&lt;Widget&gt;( w ).get() &lt;&lt; std::endl;  \t\/\/ \u0421\u0431\u0440\u043e\u0441\u0438\u043c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \tp.reset(); \t\/\/ \u0421\u0438\u043b\u044c\u043d\u044b\u0445 \u0441\u0441\u044b\u043b\u043e\u043a \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435\u0442. \u0423 weak_ptr \u0438\u0441\u0442\u0435\u043a \u0441\u0440\u043e\u043a \u0433\u043e\u0434\u043d\u043e\u0441\u0442\u0438  \t\/\/ \u041c\u0435\u0442\u043e\u0434 lock \u0441\u043d\u043e\u0432\u0430 \u0432\u0435\u0440\u043d\u0435\u0442 \u043f\u0443\u0441\u0442\u043e\u0439 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \tstd::cout &lt;&lt; __LINE__ &lt;&lt; &quot;: &quot; &lt;&lt; w.lock().get() &lt;&lt; std::endl; \t\/\/ \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 shared_ptr \u043e\u0442 \u044d\u0442\u043e\u0433\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f \u0441\u043d\u043e\u0432\u0430 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u0442 \u043a \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044e \ttry { \t\tboost::shared_ptr&lt;Widget&gt; tmp ( w ); \t} catch (const boost::bad_weak_ptr&) { \t\tstd::cout &lt;&lt; __LINE__ &lt;&lt; &quot;: bad_weak_ptr&quot; &lt;&lt; std::endl; \t} \treturn 0; } <\/code><\/pre>\n<\/div>\n<\/div>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0414\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f shared_ptr<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">#include &lt;iostream&gt;  #include &lt;boost\/thread.hpp&gt; #include &lt;boost\/shared_ptr.hpp&gt;  typedef boost::shared_mutex mutex_t; typedef boost::unique_lock&lt;mutex_t&gt; read_lock_t; typedef boost::shared_lock&lt;mutex_t&gt; write_lock_t;  mutex_t globalMutex; boost::shared_ptr&lt;int&gt; globalPtr(new int(0));  const int readThreads = 10; const int maxOperations = 10000;  boost::shared_ptr&lt;int&gt; getPtr() { \/\/ \u0417\u0430\u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0438\u0440\u0443\u0439\u0442\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443, \u0447\u0442\u043e\u0431\u044b \u0432\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0443\u043f\u0430\u043b\u043e \tread_lock_t l(globalMutex); \treturn globalPtr; }  void resetPtr(const boost::shared_ptr&lt;int&gt;& x) { \/\/ \u0417\u0430\u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0438\u0440\u0443\u0439\u0442\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443, \u0447\u0442\u043e\u0431\u044b \u0432\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0443\u043f\u0430\u043b\u043e \twrite_lock_t l(globalMutex); \tglobalPtr = x; }  void myRead() { \tfor(int i = 0; i &lt; maxOperations; ++i) {     \tboost::shared_ptr&lt;int&gt; p = getPtr(); \t} }  void myWrite() { \tfor(int i = 0; i &lt; maxOperations; ++i) { \t\tresetPtr( boost::shared_ptr&lt;int&gt;( new int(i)) ); \t} }  int main() { \tboost::thread_group tg; \ttg.create_thread( &myWrite ); \tfor(int i = 0; i &lt; readThreads; ++i) { \t\ttg.create_thread( &myRead  ); \t} \ttg.join_all(); \treturn 0; }  <\/code><\/pre>\n<\/div>\n<\/div>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0414\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b deleter + weak_ptr<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">#include &lt;string&gt; #include &lt;list&gt; #include &lt;iostream&gt; #include &lt;stdexcept&gt;  #include &lt;boost\/shared_ptr.hpp&gt; #include &lt;boost\/weak_ptr.hpp&gt; #include &lt;boost\/bind.hpp&gt;  class Connection { \tstd::string name; public: \tconst std::string& getName() const { return name; }  \texplicit Connection(const std::string& n):name(n) { \t\tstd::cout &lt;&lt; &quot;Connection &quot; &lt;&lt; name &lt;&lt; std::endl; \t}  \t~Connection() { \t\tstd::cout &lt;&lt; &quot;~Connection &quot; &lt;&lt; name &lt;&lt; std::endl; \t} };  typedef boost::shared_ptr&lt;Connection&gt; ptr_t;  class ConnectionPool { \tstd::list&lt;ptr_t&gt; connections;  \t\/\/ \u042d\u0442\u043e\u0442 \u043a\u043b\u0430\u0441\u0441 \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d \u0434\u043b\u044f \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f deleter (get1) \tclass ConnectionReleaser { \t\tstd::list&lt;ptr_t&gt;& whereToReturn; \t\tptr_t connectionToRelease; \tpublic: \t\tConnectionReleaser(std::list&lt;ptr_t&gt;& lst, const ptr_t& x):whereToReturn(lst), connectionToRelease(x) {}  \t\tvoid operator()(Connection*) { \t\t\twhereToReturn.push_back( connectionToRelease ); \t\t\tstd::cout &lt;&lt; &quot;get1: Returned connection &quot; &lt;&lt; connectionToRelease-&gt;getName() &lt;&lt; &quot; to the list&quot; &lt;&lt; std::endl;  \t\t\t\/\/ \u0417\u0430\u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0438\u0440\u0443\u0439\u0442\u0435 \u0441\u043b\u0435\u0434. \u0441\u0442\u0440\u043e\u043a\u0443 \u0438 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0440\u0430\u0437\u043d\u0438\u0446\u0443 \u0432 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0439 \u043f\u0435\u0447\u0430\u0442\u0438 \t\t\tconnectionToRelease.reset(); \t\t} \t};  \t\/\/ \u042d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0430 \u0434\u043b\u044f \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u0432\u0442\u043e\u0440\u043e\u0433\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f deleter (get2) \tstatic void releaseConnection(std::list&lt;ptr_t&gt;& whereToReturn, ptr_t& connectionToRelease) { \t\twhereToReturn.push_back( connectionToRelease ); \t\tstd::cout &lt;&lt; &quot;get2: Returned connection &quot; &lt;&lt; connectionToRelease-&gt;getName() &lt;&lt; &quot; to the list&quot; &lt;&lt; std::endl;  \t\t\/\/ \u0417\u0430\u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0438\u0440\u0443\u0439\u0442\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443 \u0438 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0440\u0430\u0437\u043d\u0438\u0446\u0443 \u0432 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0439 \u043f\u0435\u0447\u0430\u0442\u0438 \t\tconnectionToRelease.reset(); \t}  \tptr_t popConnection() { \t\tif( connections.empty() ) throw std::runtime_error(&quot;No connections left&quot;); \t\tptr_t w( connections.back() ); \t\tconnections.pop_back(); \t\treturn w; \t} public: \tptr_t get1() { \t\tptr_t w = popConnection(); \t\tstd::cout &lt;&lt; &quot;get1: Taken connection &quot; &lt;&lt; w-&gt;getName() &lt;&lt; &quot; from list&quot; &lt;&lt; std::endl; \t\tptr_t r( w.get(), ConnectionReleaser( connections, w ) ); \t\treturn r; \t}  \tptr_t get2() { \t\tptr_t w = popConnection(); \t\tstd::cout &lt;&lt; &quot;get2: Taken connection &quot; &lt;&lt; w-&gt;getName() &lt;&lt; &quot; from list&quot; &lt;&lt; std::endl; \t\tptr_t r( w.get(), boost::bind(&releaseConnection, boost::ref(connections), w )); \t\treturn r; \t}  \tvoid add(const std::string& name) { \t\tconnections.push_back( ptr_t(new Connection(name)) ); \t}  \tConnectionPool() { \t\tstd::cout &lt;&lt; &quot;ConnectionPool&quot; &lt;&lt; std::endl; \t}  \t~ConnectionPool() { \t\tstd::cout &lt;&lt; &quot;~ConnectionPool&quot; &lt;&lt; std::endl; \t} };  int main() { \tboost::weak_ptr&lt;Connection&gt; weak1; \tboost::weak_ptr&lt;Connection&gt; weak2; \t{ \t\tConnectionPool cp; \t\tcp.add(&quot;One&quot;); \t\tcp.add(&quot;Two&quot;);  \t\tptr_t p1 = cp.get1(); \t\tweak1 = p1; \t\tptr_t p2 = cp.get2(); \t\tweak2 = p2; \t} \tstd::cout &lt;&lt; &quot;Here the ConnectionPool is out of scope, but weak_ptrs are not&quot; &lt;&lt; std::endl; \treturn 0; } <\/code><\/pre>\n<\/div>\n<\/div>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0414\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0441 enable_shared_from_this<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">#include &lt;iostream&gt; #include &lt;boost\/shared_ptr.hpp&gt; #include &lt;boost\/enable_shared_from_this.hpp&gt;  class BadWidget1: public boost::enable_shared_from_this&lt;BadWidget1&gt; { public: \tBadWidget1() { \t\tstd::cout &lt;&lt; &quot;Constructor&quot; &lt;&lt; std::endl; \t\tstd::cout &lt;&lt; shared_from_this() &lt;&lt; std::endl; \t} };  class BadWidget2: public boost::enable_shared_from_this&lt;BadWidget2&gt; { public: \t~BadWidget2() { \t\tstd::cout &lt;&lt; &quot;Destructor&quot; &lt;&lt; std::endl; \t\tstd::cout &lt;&lt; shared_from_this() &lt;&lt; std::endl; \t} };  class GoodWidget: public boost::enable_shared_from_this&lt;GoodWidget&gt; { \tGoodWidget() {}  \tvoid init() { \t\tstd::cout &lt;&lt; &quot;init()&quot; &lt;&lt; std::endl; \t\tstd::cout &lt;&lt; shared_from_this() &lt;&lt; std::endl; \t} public: \tstatic boost::shared_ptr&lt;GoodWidget&gt; create() { \t\tboost::shared_ptr&lt;GoodWidget&gt; p(new GoodWidget); \t\tp-&gt;init(); \t\treturn p; \t} };   int main() { \tboost::shared_ptr&lt;GoodWidget&gt; good = GoodWidget::create(); \ttry { \t\tboost::shared_ptr&lt;BadWidget1&gt; bad1(new BadWidget1); \t} catch( const boost::bad_weak_ptr&) { \t\tstd::cout &lt;&lt; &quot;Caught bad_weak_ptr for BadWidget1&quot; &lt;&lt; std::endl; \t} \ttry { \t\tboost::shared_ptr&lt;BadWidget2&gt; bad2(new BadWidget2); \t\t\/\/ \u041f\u0440\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u0441 \u043d\u043e\u0432\u044b\u043c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043e\u043c \u0432\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0435 terminate \t\t\/\/ \u0442.\u043a. \u0441\u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0438\u0437 \u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043e \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \t} catch( const boost::bad_weak_ptr&) { \t\tstd::cout &lt;&lt; &quot;Caught bad_weak_ptr for BadWidget2&quot; &lt;&lt; std::endl; \t} \treturn 0; } <\/code><\/pre>\n<\/div>\n<\/div>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/191018\/\"> http:\/\/habrahabr.ru\/post\/191018\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\"> \t\t\t\u041a\u043b\u0430\u0441\u0441 shared_ptr \u2014 \u044d\u0442\u043e \u0443\u0434\u043e\u0431\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u0435\u0442 \u0440\u0435\u0448\u0438\u0442\u044c \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430. \u041e\u0434\u043d\u0430\u043a\u043e \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u0441\u043e\u0432\u0435\u0440\u0448\u0430\u0442\u044c \u043e\u0448\u0438\u0431\u043e\u043a, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043e\u0442\u043b\u0438\u0447\u043d\u043e \u0437\u043d\u0430\u0442\u044c \u0435\u0433\u043e \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e. \u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u043c\u043e\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043b\u0435\u0437\u043d\u0430 \u0442\u0435\u043c, \u043a\u0442\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u044d\u0442\u0438\u043c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u043c.<\/p>\n<p>  \u042f \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c:  <\/p>\n<ul>\n<li>\u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u043f\u0435\u0440\u0435\u043a\u0440\u0435\u0441\u0442\u043d\u044b\u0435 \u0441\u0441\u044b\u043b\u043a\u0438;<\/li>\n<li>\u0447\u0435\u043c \u043e\u043f\u0430\u0441\u043d\u044b \u0431\u0435\u0437\u044b\u043c\u044f\u043d\u043d\u044b\u0435 shared_ptr;<\/li>\n<li>\u043a\u0430\u043a\u0438\u0435 \u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u0434\u0441\u0442\u0435\u0440\u0435\u0433\u0430\u044e\u0442 \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 shared_ptr \u0432 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0439 \u0441\u0440\u0435\u0434\u0435;<\/li>\n<li>\u043e \u0447\u0435\u043c \u0432\u0430\u0436\u043d\u043e \u043f\u043e\u043c\u043d\u0438\u0442\u044c, \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u044f \u0441\u0432\u043e\u044e \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u0443\u044e \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u044e\u0449\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f shared_ptr;<\/li>\n<li>\u043a\u0430\u043a\u0438\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0448\u0430\u0431\u043b\u043e\u043d\u0430 enable_shared_from_this.<\/li>\n<\/ul>\n<p>  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-191018","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/191018","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=191018"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/191018\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=191018"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=191018"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=191018"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}