I am using Loki SingletonHolder in combination with Loki Factory in my project. The following example basically consists of two开发者_如何学Python 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.
精彩评论