Shared_from_this throws an exception

I am writing a Qt-based application with functions similar to Blender.

It consists of a "framework", which is a GUI + plugin system and plugins. Plugins are Qt dlls with objects (for example, Sphere, Box, etc.), which can be basically created and displayed. All those objects that were once created are stored in a structure in some kind of container structure that contains shared_ptr for them (so in fact the container is almost similar to vector<shared_ptr<INode>>)

I want to use the shared_from_this () function inside one of the plugins. For instance. Here's an example plugin (code modified for clarity):

class Q_DECL_IMPORT SphereNode: public INode, public Sphere

Where INode:

class INode: public QObject, public boost::enable_shared_from_this<INode>

base class for everything that is stored in the container. Therefore, the problem is that this function:

void SphereNode::update()
{
 foo(shared_from_this());
}

boost::bad_weak_ptr.

, SphereNode ( Factory)

boost::shared_ptr<INode> NodeFactory::createNode(const QString& type, QString tag)
{
...
QPluginLoader loader(filesPlugin_[i]);
boost::shared_ptr<QObject> plugin(loader.instance());
boost::shared_ptr<INode> iNodePlugin = boost::shared_dynamic_cast<INode>(plugin);
return iNodePlugin;
}

?

+3
1

, :

boost::shared_ptr<INode> iNodePlugin = boost::shared_dynamic_cast<INode>(plugin);

:

boost::shared_ptr<INode> iNodePlugin = dynamic_cast<INode*>(loader.instance())->shared_from_this();

, - :

boost::shared_ptr<QObject> plugin(loader.instance());

plugin . , Qt , QPluginLoader .

segfault ( undefined), boost::bad_weak_ptr.

, null_deleter, , 0.


shared_from_this() ( )?

, .

, , shared_ptr .

, shared_ptr factory ( ), .

+2

Source: https://habr.com/ru/post/1753858/


All Articles