在基类崩溃中调用虚拟功能

Call to virtual function in base class crash

本文关键字:虚拟 功能 调用 基类 崩溃      更新时间:2023-10-16

我在公共递归中呼叫一个受保护的虚拟功能的问题。

我想拥有的是 base class class class,它调用递归函数 getAllStrings(),然后将其称为 virtual getTring()在其"儿童"列表的所有项目上函数。
getTring()方法仅在其某些孩子类中实现。

//base.hpp

class Base
{
private:
    // SharedBase is a typedef boost::shared_ptr<Base>
    std::list<SharedBase> baseList;
protected:
    virtual std::string getString() const
    {
        return "";
    };
public:
    Base(){}
    virtual ~Base(){}
    void addElmtToList(SharedBase sb)
    {
        baseList.push_back(sb);
    }
    // recursive
    void getAllStrings(std::string &returnedString)
    {
        if (!baseList.empty())
        {
            // Iterate through all children
            std::list<SharedBase>::iterator itBase = baseList.begin();
            while (itBase != baseList.end())
            {
                (*itBase)->getAllStrings(returnedString);
                ++itBase;
            }
        }
        returnedString += getString(); // ERROR !
    }
};

//derived_a.hpp

class Derived_a : public Base
{
public:
    Derived_a();
    virtual ~Derived_a();
protected:
    std::string getString() const
    {
        return "derived_a string";
    }
};

//derived_b.hpp

class Derived_b : public Base
{
public:
    Derived_b();
    virtual ~Derived_b();
};

因此,基本上的应用程序呼叫 getallStrings(),除了调用 getString() ,使应用程序崩溃。

简单地调用 getString()函数,而无需任何递归率(注释 If block)使应用程序崩溃。

将其放置在非虚拟上使其正常工作,但是在这种情况下,功能是没有用的,因此我有点迷路了。为什么崩溃?

编辑10/03:在此处添加SSCCE。基于Synxis代码。在我的计算机上使用Boost Linked工作。所以我想这确实与我们的应用程序环境有关。

我终于解决了这个问题。

我用drmingw做了一个跟踪,显示在 ntdll.dll!RtlUlonglongByteSwap

中segfault之前调用了信号

但是,该函数中没有使用信号。

这是罪魁祸首:

//在" base.hpp"文件中

protected:
    /// Some useful type definition for the signal to emit when the
    /// configuration changes.
    typedef boost::signals2::signal<void ()> OnConfigurationUpdated;
    virtual std::string getString()
    {
       return "";
    }
public:
    /// Signal that subscribers have to connect to if they want to be alerted
    /// about the configuration changes.
    OnConfigurationUpdated reactOnChanges;

该项目使用另一个项目序列化,其中类别是导入类的地方。但是,该项目并未与GUI重建。从序列化项目中正确清洁(删除.o和.dll文件)使其有效。