[bind] Doubt regarding the use of shared_ptr with boost.bind
Hi, I have a doubt on using bind library. I pass the output of Bind to Asio library, and at certain point of time another thread will ask Windows I/O service to execute the function append(): m_ioService.post(m_strand.wrap( boost::bind(&LogWriter::append, (*logWritersItr), _logMessage, _logLevelString) )); The second param of Bind, (*logWritersItr), is a shared_ptr of LogWriter's subclass. The signature of LogWriter::append() is as following: virtual void append(const std::string& _logMessage, const std::string& _logLevelString) = 0; Base on document at http://www.boost.org/doc/libs/1_44_0/libs/bind/bind.html, the way I use bind will duplicate a copy of that shared_ptr. Any idea when will the duplicated shared_ptr become invalid so that the reference count decreases by one? Thanks, Shiou Ming
m_ioService.post(m_strand.wrap( boost::bind(&LogWriter::append, (*logWritersItr), _logMessage, _logLevelString) ));
The second param of Bind, (*logWritersItr), is a shared_ptr of LogWriter's subclass. The signature of LogWriter::append() is as following:
virtual void append(const std::string& _logMessage, const std::string& _logLevelString) = 0;
Base on document at http://www.boost.org/doc/libs/1_44_0/libs/bind/bind.html, the way I use bind will duplicate a copy of that shared_ptr. Any idea when will the duplicated shared_ptr become invalid so that the reference count decreases by one?
bind() takes your shared_ptr by value, so it won't become "invalid". After io_service executes your functor, it will be destroyed, so all its internal copies of that shared_ptr will be destroyed as well.
On Thu, Sep 23, 2010 at 9:25 AM, Igor R
m_ioService.post(m_strand.wrap( boost::bind(&LogWriter::append, (*logWritersItr), _logMessage, _logLevelString) ));
The second param of Bind, (*logWritersItr), is a shared_ptr of LogWriter's subclass. The signature of LogWriter::append() is as following:
virtual void append(const std::string& _logMessage, const std::string& _logLevelString) = 0;
Base on document at http://www.boost.org/doc/libs/1_44_0/libs/bind/bind.html, the way I use bind will duplicate a copy of that shared_ptr. Any idea when will the duplicated shared_ptr become invalid so that the reference count decreases by one?
bind() takes your shared_ptr by value, so it won't become "invalid". After io_service executes your functor, it will be destroyed, so all its internal copies of that shared_ptr will be destroyed as well. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
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?
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?
This is a feature of asio::io_service. By saying "functor" I mean the function-object that bind() returns. This object stores your shared_ptr as a member-data. You pass this object to io_serivce::post(), and when io_service is done with this function-object, io_service removes it from the internal queue - so the object is being destroyed with all its members.
On Sep 23, 2010, at 9:59 AM, Shiou Ming Lee wrote:
On Thu, Sep 23, 2010 at 9:25 AM, Igor R
wrote: m_ioService.post(m_strand.wrap( boost::bind(&LogWriter::append, (*logWritersItr), _logMessage, _logLevelString) ));
The second param of Bind, (*logWritersItr), is a shared_ptr of LogWriter's subclass. The signature of LogWriter::append() is as following:
virtual void append(const std::string& _logMessage, const std::string& _logLevelString) = 0;
Base on document at http://www.boost.org/doc/libs/1_44_0/libs/bind/bind.html, the way I use bind will duplicate a copy of that shared_ptr. Any idea when will the duplicated shared_ptr become invalid so that the reference count decreases by one?
bind() takes your shared_ptr by value, so it won't become "invalid". After io_service executes your functor, it will be destroyed, so all its internal copies of that shared_ptr will be destroyed as well. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
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?
I think that you're thinking too hard about this. It's not a feature from boost.bind, it's just standard C++ lifetime rules. 1) The shared_ptr is copied into the bind object. (use count for the shared_ptr ++) 2) The bind object is passed (by value) to m_ioService.post. (use count for the shared_ptr++) *) The original bind object gets destroyed (goes out of scope, whatever) (use count for the shared_ptr--) *) When m_ioService.post returns, the copy of the bind object that was passed to it is destroyed (use count for the shared_ptr--) the last two are '*'s rather than 3/4 since I don't remember the order (and it doesn't really matter, either). ;-) 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. -- Marshall
On Fri, Sep 24, 2010 at 1:37 AM, Marshall Clow
On Sep 23, 2010, at 9:59 AM, Shiou Ming Lee wrote:
On Thu, Sep 23, 2010 at 9:25 AM, Igor R
wrote: m_ioService.post(m_strand.wrap( boost::bind(&LogWriter::append, (*logWritersItr), _logMessage, _logLevelString) ));
The second param of Bind, (*logWritersItr), is a shared_ptr of LogWriter's subclass. The signature of LogWriter::append() is as following:
virtual void append(const std::string& _logMessage, const std::string& _logLevelString) = 0;
Base on document at http://www.boost.org/doc/libs/1_44_0/libs/bind/bind.html, the way I use bind will duplicate a copy of that shared_ptr. Any idea when will the duplicated shared_ptr become invalid so that the reference count decreases by one?
bind() takes your shared_ptr by value, so it won't become "invalid". After io_service executes your functor, it will be destroyed, so all its internal copies of that shared_ptr will be destroyed as well. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
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?
I think that you're thinking too hard about this. It's not a feature from boost.bind, it's just standard C++ lifetime rules.
1) The shared_ptr is copied into the bind object. (use count for the shared_ptr ++) 2) The bind object is passed (by value) to m_ioService.post. (use count for the shared_ptr++) *) The original bind object gets destroyed (goes out of scope, whatever) (use count for the shared_ptr--) *) When m_ioService.post returns, the copy of the bind object that was passed to it is destroyed (use count for the shared_ptr--)
the last two are '*'s rather than 3/4 since I don't remember the order (and it doesn't really matter, either). ;-)
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.
-- Marshall
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Got it. Thanks Igor and Marshall. :)
participants (3)
-
Igor R
-
Marshall Clow
-
Shiou Ming Lee