The problem is that you have a race condition between the
creation and the instantiation of the object. There are two
possible solutions; you can synchronize the GetUI
function,
as Jerry YY suggested, or you can ensure that the singleton is
created before you start threading; a race condition only occurs
when you modify the object in at least one thread, and once
you've created the object, instance
is never modified.
One way of doing this is to simply define Interface::instance
as:
Object* Interface::instance = Interface::GetUI();
Zero initialization ensures that before Interface::GetUI
is
called, Interface::instance
is initialized to the null
pointer, and the initialization of static objects takes place
before main
.
Otherwise: if you're sure that Interface::GetUI
will never be
called before entering main
(which seems likely, given what
you've said—there shouldn't be any mouse events before you
enter main
), then you can drop the test (or replace it with
assert( instance != nullptr
) in Interface::GetUI
, and just
write:
Object* Interface::instance = new Interface();
Much simpler, and avoids all of the problems.