I am trying to share_ptr recently and I have run something in a strange case. The template member function which I want is able to return a shared _pitter of its derivative type. I'm running Visual Studio 2010, which has access to some new C ++ 0x standards, but I think shared shared APTR behaves equally.
This works fine with the Pure Pointer I just have a dynamic_cast < DerivedClass * & gt; (It) has returned. I'm stumped in a way here, however, by enabling, also attempts to remove itself after enabled_shared_frame_his object is called the function (which is bad). I'm probably going to make this mistake, but I want to work to imitate the jobless equivalent (which was working) to do the following work (this is the code in which I have trouble).
// Assume that we have a virtual function class base class: public stat :: enabled_space ____________ the_based_language; & Lt; Base Class & gt; {.... Template & lt; Typename DerivedClass & gt; Std :: shared_ptr & lt; DerivedClass & gt; Base Class :: getThis () {// I return the typing std :: dynamic_pointer_cast & lt; DerivedClass & gt; (Shared_from_this ()) had to put some code here to make sure; }} Edit : It seems that the function works properly, the problem was how I was using it. This is bad, for example, do the following: std :: shared_ptr> DerivedClass & gt; P = std :: make_shared & lt; DerivedClass & gt; (); P-> GetType & LT; DerivedClass & gt; - & gt; SomeOtherFunctionOnlyInTheDerivedClass (); This is not a problem:
std :: shared_ptr GetType & LT; DerivedClass & gt; - & gt; SomeOtherFunctionOnlyInTheDerivedClass (); I am not completely sure that this is the problem of changing the same type, or reference count problem. In any case, I was doing something dumb and broke it, to avoid unnecessary getType calls at that time, everything works fine in every other case. I'm using it to be that someone can really explain What is the first example to break with the work of another example. I give the answer to that answer.
To expand on the reply of Stuart and (possibly) tell why you were crashed, My best estimate is that you were calling getType on a stack-allocated example, it's a big threat to use enable_shared_from_this . #include & lt; Memory & gt; Class base: public station :: enabled_shard_frame_this & lt; Base & gt; {Public: Virtual ~ Base () {} Template & lt; Typename D & gt; Std :: shared_ptr & lt; D & gt; GetType () {return std :: dynamic_pointer_cast & lt; D & gt; (Shared_from_this ()); }}; Category received: public base {public: zero F () {}}; Int main () {std :: shared_ptr & lt; Derived & gt; D = std :: make_shared & lt; Derived & gt; (); D & gt; GetType & LT; Derivative & gt; () - & gt; F (); // fine-derived D2; Base * p = & amp; D2; P-> GetType & LT; Derived () & gt; - & gt; F (); // F () returns after attempting to remove D2 will return 0; } The reason for this is because the reference number of d2 is zero when it is on the stack calling shared_from_this a shared_ptr < / Code> which increases the reference number to one. Once this indicator goes out of the scope, it reduces its count in zero, which then tries to remove the example, which is definitely on the stack is. The only way I can think of protecting myself is by doing this, on top of my head, all the constructors are protected or made private and providing static functions which are dynamically Also assign an example, shared_ptr s, back,
Comments
Post a Comment