<br><div class="gmail_quote">On Fri, Sep 24, 2010 at 1:37 AM, Marshall Clow <span dir="ltr">&lt;<a href="mailto:mclow.lists@gmail.com">mclow.lists@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div style="word-wrap: break-word;"><div><div></div><div class="h5"><div><div>On Sep 23, 2010, at 9:59 AM, Shiou Ming Lee wrote:</div><blockquote type="cite"><br><br><div class="gmail_quote">On Thu, Sep 23, 2010 at 9:25 AM, Igor R <span dir="ltr">&lt;<a href="mailto:boost.lists@gmail.com" target="_blank">boost.lists@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div>&gt; m_ioService.post(m_strand.wrap(<br>
&gt; ��������������������������� boost::bind(&amp;LogWriter::append,<br>
&gt; (*logWritersItr), _logMessage, _logLevelString)<br>
&gt; ������������������������ ));<br>
&gt;<br>
&gt; The second param of Bind, (*logWritersItr), is a shared_ptr of LogWriter&#39;s<br>
&gt; subclass. The signature of LogWriter::append() is as following:<br>
&gt;<br>
&gt; virtual void append(const std::string&amp; _logMessage, const std::string&amp;<br>
&gt; _logLevelString) = 0;<br>
&gt;<br>
&gt; Base on document at<br>
&gt; <a href="http://www.boost.org/doc/libs/1_44_0/libs/bind/bind.html" target="_blank">http://www.boost.org/doc/libs/1_44_0/libs/bind/bind.html</a>, the way I use bind<br>
&gt; will duplicate a copy of that shared_ptr. Any idea when will the duplicated<br>
&gt; shared_ptr become invalid so that the reference count decreases by one?<br>
<br>
</div>bind() takes your shared_ptr by value, so it won&#39;t become &quot;invalid&quot;.<br>
After io_service executes your functor, it will be destroyed, so all<br>
its internal copies of that shared_ptr will be destroyed as well.<br>
_______________________________________________<br>
Boost-users mailing list<br>
<a href="mailto:Boost-users@lists.boost.org" target="_blank">Boost-users@lists.boost.org</a><br>
<a href="http://lists.boost.org/mailman/listinfo.cgi/boost-users" target="_blank">http://lists.boost.org/mailman/listinfo.cgi/boost-users</a></blockquote></div><br>I think I don&#39;t quite completely understand functor.., is it by C++ that a functor will be destroyed automatically once executed? Or is this a feature from boost.bind?</blockquote>
<br></div></div></div><div>I think that you&#39;re thinking too hard about this.</div><div>It&#39;s not a feature from boost.bind, it&#39;s just standard C++ lifetime rules.</div><div><br></div><div>1) The shared_ptr is copied into the bind object. (use count for the shared_ptr�++)</div>
<div>2) The bind object is passed (by value) to�m_ioService.post. �(use count for the shared_ptr++)</div><div>*) The original bind object gets destroyed (goes out of scope, whatever) �(use count for the shared_ptr--)</div>
<div>*) When�m_ioService.post returns, the copy of the bind object that was passed to it is destroyed�(use count for the shared_ptr--)</div><div><br></div><div>the last two are &#39;*&#39;s rather than 3/4 since I don&#39;t remember the order (and it doesn&#39;t really matter, either). ;-)</div>
<div><br></div><div>If�m_ioService.post makes copies of the functor and/or the shared pointer internally, then the use count for the shared pointer will be incremented for each copy, and decremented when a copy is destroyed.</div>
<div><br></div><font color="#888888"><div><br></div><div>-- Marshall</div><div><br></div></font></div><br>_______________________________________________<br>
Boost-users mailing list<br>
<a href="mailto:Boost-users@lists.boost.org">Boost-users@lists.boost.org</a><br>
<a href="http://lists.boost.org/mailman/listinfo.cgi/boost-users" target="_blank">http://lists.boost.org/mailman/listinfo.cgi/boost-users</a><br></blockquote></div><br><br>Got it. Thanks Igor and Marshall. :)<br>