Since i always wanted to look at Loki libraries i used this chance. Well, to start, the issue is not connected with MSVC versions, i did run the same example on VS2008 and Loki libraries build with VS2008 and had the same results. Second:
#include "stdafx.h"
#include <loki/Factory.h>
#pragma comment(lib, "loki_D.lib")
struct Base{};
struct Derived : public Base{};
Base* buildDerived(){
return new Derived();
}
int _tmain(int argc, _TCHAR* argv[])
{
Loki::Factory< Base, int> factory;
bool registeredD = factory.Register(1, buildDerived);
//system("pause");
return 0;
}
This works fine. I examined the crash at your example and seems that the Factory is deleting the association container where the functor you created is supposed to be after it is already destroyed. Seems to me that this is because Loki is hooking up atexit()
and deleting some stuff there (not sure why, I guess to handle Singleton objects), that is where your Functor gets deleted, and then after the the destructor of your factory is called and then the erase
call on the association container fails. Well - if it's not a huge deal, don't make the factory a global object. If it is a huge deal, try to debug and find why Loki is doing the stuff it does in atexit
, maybe you need to set up something more. But at least as far as I can see, this is another bad case of undefined behaivor for destruction of global objects.
EDIT: Loki Factory-Singleton throws "dead reference detected" in try-catch-block on ARM , the macro LOKI_FUNCTOR_IS_NOT_A_SMALLOBJECT could be relevant, since I would guess that triggers the destrcution, maybe without this the object will not get in the list of objects destroyed in atexit
but still, my knowledge about Loki libraries is not good enough. Maybe you need to define a policy of lifetime of the singleton or something like that.