Loki Factory-Singleton throws “dead reference detected” in try-catch-block on ARM

你。 提交于 2019-12-24 07:31:04

问题


I am using Loki SingletonHolder in combination with Loki Factory in my project. The following example basically consists of two lines of code in the try-block to 1.) use the factory 2.) then throw an exception.

#include <iostream>
#include <loki/Singleton.h>
#include <loki/Factory.h>

class AbstractBase {};
class ConcreteChild : public AbstractBase {};

class TestFactory: public Loki::SingletonHolder< Loki::Factory<AbstractBase, std::string>  >
{};

template <class T>
T* createNew()
{
    return new T();
}

int main(int argc, char *argv[])
{

    try
    {
        TestFactory::Instance().Register("ConcreteChild", createNew<ConcreteChild >);
        throw std::runtime_error("test exception");
    }
    catch (std::exception& e)
    {
        std::cout << "EXCEPTION: " << e.what();
        return -1;
    }
    return 0;
}

Running the code on a Linux PC (x86) I get "EXCEPTION: test exception" as the output.

However, if I cross-compile the code with arm-angstrom-linux-gnueabi-g++ and then run the program on a BeagleBoard (ARM processor), the program exits with:

ERROR - EXCEPTION: test exception
terminate called after throwing an instance of 'std::logic_error'
  what():  Dead Reference Detected
Aborted

I changed the program to

//same as above, only different main():

int main(int argc, char *argv[])
{

    TestFactory::Instance().Register("ConcreteChild", createNew<ConcreteChild >);
    throw std::runtime_error("test exception");
    return 0;
}

and it exits with

terminate called after throwing an instance of 'std::runtime_error'
  what():  test exception
Aborted

So it seems like there is no dead reference here.

Do you have any hints on how I could debug this problem? Or any ideas what might cause this behaviour?

Thanks a lot!


EDIT: the problem even occurs if I do not throw the exception

int main(int argc, char *argv[])
{

    try
    {
        TestFactory::Instance().Register("ConcreteChild", createNew<ConcreteChild >);
    }
    catch (std::exception& e)
    {
        std::cout << "EXCEPTION: " << e.what());
        return -1;
    }
    return 0;
}

EDIT 2: The problem disappears by defining LOKI_FUNCTOR_IS_NOT_A_SMALLOBJECT. Obviously the dead reference is not caused by "my" Singleton (because it also occurs with a custom LifetimePolicy that doesn't throw) but by the AllocatorSingleton of SmallObj.h which is used by Factory. However, the question remains why this only happens on the ARM system only and how it could be avoided without the define mentioned above.

来源:https://stackoverflow.com/questions/6858712/loki-factory-singleton-throws-dead-reference-detected-in-try-catch-block-on-ar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!