Dragonfly Q&A

Could a singleton be the possible cause of a segfault (core dump)?

0 like 0 dislike
We are using a singleton to manage a number of game processes, and this singleton in question is also derived from the object base class. When we shutdown the game manager we immediatly get a seg fault. Is it possible that when the singleton in question is deleted by the engine it is causing a seg fault?

If not, I'm really stumped, I was able to narrow down the seg fault to the shutdown process using gdb, but Im not sure of where to go from here.
asked Oct 14, 2014 by remckenna
So I've done some thinking, and it would seem to me that singleton is never actually preceded by "new" which means it can't actually be deleted. Although it would still be "added" to the list of game objects which gets deleted, although that list is of pointers and this singleton never actually takes the form of a pointer... is this thinking correct?

1 Answer

1 like 0 dislike

If you create a singleton as we did for the Managers, as a static locally declared variable like:

  static myClass single;

  return single;

then, yes, when the engine goes to delete it, it will segfault.  You can't call delete on an object allocated off the stack - it has to be allocated via new.  What can do is something like:

  static myClass *p_single = new myClass();

  return p_single;

adjusting the signature to return a pointer to myClass.  Once done, when the world manager deletes the object upon shutdown, it can do so and not fault since it was allocated via "new".

answered Oct 14, 2014 by claypool
More information about Dragonfly can be found on the Dragonfly Home Page.
...