How to deal with “exit-time destructor” warning in clang?

后端 未结 1 674
隐瞒了意图╮
隐瞒了意图╮ 2020-12-30 19:29

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

static const std::map

        
相关标签:
1条回答
  • 2020-12-30 19:58

    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.

    0 讨论(0)
提交回复
热议问题