Do cleanup before exit

Issue #266 duplicate
Shibe created an issue

I'm withdrawing my pull request and continuing the discussion here.

I think that this is a better design than doing cleanup using atexit. The reason is that libraries may also use atexit, and it may be difficult to ensure the correct cleanup order for all platforms and versions of libraries (including future versions). For example, with the current design, all cleanup functions are called in one atexit handler, and they generally can't be ordered correctly with other atexit handlers.

Global object destructors are called after exit too, so they should not call library functions that may be unsafe to call during exit.

atexit and similar techniques should still be used for things we want to clean up which the system does not clean up automatically. Of course, such cleanup functions must check if the cleanup is already done.

I tried to find places of exit in the current code:

  • CheckParameters
  • DoScriptMessage does exit(0); is this correct?
  • InitGame
  • Language::ReadLump does exit(0); is this correct?
  • Net::Init
  • Quit
  • WL_Main
  • main return

Comments (2)

  1. Braden Obrzut

    DoScriptMessage doesn’t ever actually call exit since the message handler is replaced. scanner.cpp/h is shared across a few projects so that’s just how the default behavior is (hence all the junk about SCString and what not). That said the function could be improved to call exit in DefaultMessageHandler instead and then use TUniquePtr to keep the memory allocation clean. Honestly though I should probably just remove the generic parts since almost all of the other projects using the code have since diverged slightly anyway. In which case FString::Format could be used instead of sprintf.

    Have no idea why Language::ReadLump isn’t just using Scanner::ScriptMessage though. I don’t think that ever didn’t exist.

  2. Log in to comment