<br><div class="gmail_quote">On Fri, Sep 24, 2010 at 1:37 AM, Marshall Clow <span dir="ltr"><<a href="mailto:mclow.lists@gmail.com">mclow.lists@gmail.com</a>></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"><<a href="mailto:boost.lists@gmail.com" target="_blank">boost.lists@gmail.com</a>></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>> m_ioService.post(m_strand.wrap(<br> > ��������������������������� boost::bind(&LogWriter::append,<br> > (*logWritersItr), _logMessage, _logLevelString)<br> > ������������������������ ));<br> ><br> > The second param of Bind, (*logWritersItr), is a shared_ptr of LogWriter's<br> > subclass. The signature of LogWriter::append() is as following:<br> ><br> > virtual void append(const std::string& _logMessage, const std::string&<br> > _logLevelString) = 0;<br> ><br> > Base on document at<br> > <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> > will duplicate a copy of that shared_ptr. Any idea when will the duplicated<br> > 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't become "invalid".<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'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're thinking too hard about this.</div><div>It's not a feature from boost.bind, it'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 '*'s rather than 3/4 since I don't remember the order (and it doesn'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>