Re: [asio-users] this vs shared_from_this() in examples
Brought to you by:
chris_kohlhoff
From: Yuri T. <yu...@ti...> - 2012-02-21 17:38:54
|
I usually use shared_ptr and sometimes weak_ptr comes handy. If you want to finish operation - you should do it manually from outside (somehow): close sockets, cancel timers (and handle successive read errors), send teardown request (and when close socket), etc. I really don't see much problems with always using shared_from_this. If your destructor is never called check that you have a correct way to shut your session down. Anyways, since C++ doesn't have garbage collector you should have (event with use of smart pointers) a clear understanding of how your component operates and lifetime of all calls/handlers, etc. Regards, Yuri On Tue, Feb 21, 2012 at 2:07 PM, Igor R <boo...@gm...> wrote: > > In my project ( http://gitorious.org/libbitcoin/libbitcoin ), I just > > realised today I have a problem with circular references. When I started, > > someone advised me to just use shared_ptr for bind and never use this. > > Assuming asio is fine with this, I didn't question that logic. However > now > > I'm checking and seeing my destructors never get called. > > > > Looking through the boost asio examples, I see bind is used with this and > > shared_from_this() variously. I cannot see any pattern to it? What are > the > > rules of thumb for when to use this? Why can I not just use this for > > everything? Would you pass this to std::bind when you know that the > > primitives inside it (like the strand if you're using strand post) are > > destroyed before the class? > > > > What happens then when I use strand.wrap(std::bind(&foo::memfunc, this, > ...)) and pass that elsewhere? > > > The question is how you ensure that the object bound to your > completion handler will outlive the async operation. > Binding shared_ptr to "this" is the most simple and robust way to > provide such guarantee in a complex design, but in some trivial cases > it can be unnecessary -- these are probably the cases you saw in asio > examples. > As for circular references, this issue is usually solved by means of > using weak_ptr in one direction. > > > ------------------------------------------------------------------------------ > Keep Your Developer Skills Current with LearnDevNow! > The most comprehensive online learning library for Microsoft developers > is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3, > Metro Style Apps, more. Free future releases when you subscribe now! > http://p.sf.net/sfu/learndevnow-d2d > _______________________________________________ > asio-users mailing list > asi...@li... > https://lists.sourceforge.net/lists/listinfo/asio-users > _______________________________________________ > Using Asio? List your project at > http://think-async.com/Asio/WhoIsUsingAsio > |