55

In my C++11 code I get the clang warning "Declaration requires an exit-time destructor" in the following case:

static const std::map<int, const someStruct> mymap = {
    {1, {
        "A",
        "B",
        "C"
    }},
    {2, {
        "D",
        "E",
        "F"
    }}
};

As far as I understand Google an "exit-time destructor" is required to destroy main() and statics in a deterministic way to prevent crashes on exit due to "already released variables". Is that right? Can someone explain it better?

Plus: What can I do about it (I don't want to disable the warning)? The code above is used within context of one thread only.

Looks like this is the way Chromium deals with these cases; would that be the right way for my case as well?

#define CR_DEFINE_STATIC_LOCAL(type, name, arguments) \
  static type& name = *new type arguments

(Source: https://chromium.googlesource.com/chromium/src/+/32352ad08ee673a4d43e8593ce988b224f6482d3/base/basictypes.h)

Tom
  • 1,203
  • 4
  • 21
  • 36
Lars Schneider
  • 5,530
  • 4
  • 33
  • 58

2 Answers2

33

Global and function static objects will get their destructors called when your application is exiting. these destructors are "exit time destructors". and are called in the reverse order that they were constructed in.

As you said, if some of these destructors touch already destroyed objects, your program could crash. Also, destructors running at exit time will make the program exit slower, and most of the time they are not necessary for the correctness of the program (since when the program exits, it'll release all its memory anyway).

The warning is simply pointing out that you have destructors that'll be run at exit time.

The fix you proposed will heap allocate the object, which will not cause it to be automatically destroyed at program exit. For your case, this is probably good enough.

yiding
  • 3,482
  • 18
  • 17
  • Depending on what `someStruct`’s destructor does. I’m generally wary of preventing destructors from being run. Managing memory might not be an issue any more at program exit, but other non-trivial operations might have to run nevertheless. In a more complex scenario it quickly becomes hard to prove that not running destructors has no averse effect. Furthermore, the slowdown at program exit is noticeable only when large portions of memory get released. – Konrad Rudolph Jan 15 '13 at 10:45
  • 1
    Yep, that's why i say "probably" and "most of the time". If your destructors interact with the rest of the system via some sort of IO, then you want these destructors to run. A simple example would be a global configuration object which saves the settings to disk when the program is quitting, and you rely on the destructor to know when that happens (not that I advocate for code that does this). – yiding Jan 15 '13 at 11:03
  • 3
    @KonradRudolph The struct is three "const char *". Therefore my co-worker rightly objects that heap allocation is overkill. Is there any way to make this warning free without heap allocation? – Lars Schneider Jan 15 '13 at 13:25
  • 1
    I think it's complaining about the std::map, rather than the structs inside it. – yiding Jan 15 '13 at 22:28
  • 1
    @yiding True. But is there anything I can do about it besides the heap allocation #define in my question? – Lars Schneider Jan 20 '13 at 10:42
  • You should just let the profiler tell you this stuff and disable the warning. It is not even in pedantic, after all. – thesaint Jun 02 '13 at 07:12
  • Would it be correct to edit the last paragraph to say "which will cause it to not be automatically destroyed"? It's not to be pedantic, but because it changes the meaning of the sentence a bit, and I want to make sure I understand it correctly :) – Svend Hansen Mar 01 '21 at 21:01
4

See attributes no_destroy/always_destroy https://clang.llvm.org/docs/AttributeReference.html