如何处理“退出时间析构函数”警告在ang? [英] How to deal with "exit-time destructor" warning in clang?

查看:3615
本文介绍了如何处理“退出时间析构函数”警告在ang?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的C ++ 11代码中,我得到clang警告声明需要一个退出时间析构函数在下面的情况下:

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"
    }}
};

据我所知,Google需要一个退出时析构函数来销毁main以确定性方式防止由于已释放变量而导致的退出崩溃。是对的吗?有人可以更好地解释吗?

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.

看起来这是Chromium处理这些情况的方式;

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

(来源: http://src.chromium.org/svn/trunk/src/base/basictypes.h

推荐答案

当应用程序退出时,全局和函数静态对象将获取它们的析构函数。这些析构函数是退出时间析构函数。

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.

这篇关于如何处理“退出时间析构函数”警告在ang?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆