1

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.

Philipp
  • 11,549
  • 8
  • 66
  • 126
  • So, I guess Loki is what is throwing the logic error. It frankly looks as though there is a cross-compiler issue here. I would try to find where (within Loki) the exception is being thrown from. – John Jul 28 '11 at 12:23
  • as far as I understand, it is thrown in line 819 of http://loki-lib.sourceforge.net/html/a00670.html Sadly, I cannot debug on the ARM device to find out what exactly happens. – Philipp Jul 28 '11 at 12:52
  • Interesting. Destroyed is only set to true on line 841. – John Jul 28 '11 at 15:45
  • Two things: Are you the one who brought in the Loki library? (ie., are you sure the Singleton.h looks exactly like what's on sourceforge?). Second thing: Instead of generating an executable (-o something) use the -E option and redirect standard output to a file (-E > something.cpp). This will give you the preprocessed source. The templates will not be expanded, but if some widked macro redefinition is occurring you'll be able to see it. The more I think about, the more likely that this seems to be your problem. – John Jul 28 '11 at 15:49

0 Answers0