Boost logo

Boost Users :

Subject: [Boost-users] enabled_shared_from_this: bad_weak_ptr exception + online doc explanation
From: cxv_at_[hidden]
Date: 2008-09-05 16:54:37


Hi all,

I have a class called TextTool. In its constructor I initialize a member
variable. This member variable needs a pointer to TextTool in
*its*constructor. Hence I need to have a pointer to "this" (TextTool)
and then
pass it to the constructor of the member variable. This is a summery of my
code:

*TextTool.h*
class TextTool : public Tool, public
boost::enable_shared_from_this<TextTool>
{
public:
    TextTool(QDialog *taskDialogue);
private:
    boost::shared_ptr<ToggleControl> bold; //Member variable that needs a
pointer to TextTool in its constructor
};

*TextTool.cpp*
TextTool::TextTool(QDialog *taskDialogue) : Tool(this->name)
{
    boost::shared_ptr<TextTool> pointerToThis =
TextTool::shared_from_this(); //ERROR OCCURS ON THIS LINE

    bold = boost::shared_ptr<ToggleControl>(new
ToggleControl(pointerToThis));
}

I get a runtime exception: "boost::bad_weak_ptr at memory location
0x0012fccc" when executing the above mentioned line.
On the BOOST online documentation, I dont understand what this line means:
"**this* must be a subobject of an instance *t* of type *T* . There must
exist at least one *shared_ptr* instance *p* that *owns* *t*.". Do I need
to have an instance (t) of TextTool (T) as a member variable of the class
TextTool? Won't this lead to loops in my situation (since I want the "this"
pointer to be created in the constructor itself)?

Any help?
Thanks,
/C.



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net