简体   繁体   English

静态对象破坏时崩溃

[英]Crash at static object destruction

I am working on a 3rd party c++ app. 我正在开发第三方C ++应用程序。 It is crashing during the exit. 在退出过程中它崩溃了。 If I look at the stack all I get is the __static_initialization_and_destruction_0 function and lots of questions marks. 如果看一下堆栈,我得到的只是__static_initialization_and_destruction_0函数和很多问号。 Project is huge and unfortunately it has many static objects. 项目很大,不幸的是它有很多静态对象。 Is there any way to find out which one is crashing? 有什么办法找出哪一个崩溃了吗?

Although, this is probably not the advice you are looking for, avoid the use of static objects. 虽然,这可能不是您要查找的建议,但请避免使用静态对象。 The reason for this is that there is no way to guarantee the order of construction and destruction. 这样做的原因是,没有办法保证构造和破坏的顺序。

I am guessing here but it is entirely possible that one static object depends on another static object. 我在这里猜测,但是一个静态对象完全有可能依赖于另一个静态对象。 Since there is no way of guaranteeing the order of destruction, you are ending up in trouble. 由于无法保证销毁顺序,因此您将陷入困境。

It may well be worth your while to change your static objects to pointers that you create at the beginning of you main function and destroy at the end of your main function. 最好将静态对象更改为在主函数开始时创建的指针,然后在主函数结束时销毁的指针。 You then have the option of ordering them appropriately. 然后,您可以选择适当订购它们。

If you can, run with a debugger attached and it'll let you break at the point of the crash. 如果可以,请运行附带调试器的调试器,它可以让您在崩溃时中断。

Otherwise, you might try adding logging information in the destructors, such as: 否则,您可以尝试在析构函数中添加日志记录信息,例如:

std::cout << "In ~SomeObject." << std::endl;

The invalid call stack may indicate that the stack has been corrupted by one of the destructors. 无效的调用堆栈可能表明该堆栈已被析构函数之一破坏。 I suggest that you place a breakpoint in every destructor for which a static object is instantiated, then you may determine which was the last destructor that executed. 我建议您在要实例化静态对象的每个析构函数中放置一个断点,然后可以确定哪个是最后执行的析构函数。

Also at each break, you should observe the call stack for signs that it has been corrupted. 同样,在每个中断处,您都应观察调用堆栈以了解其已损坏的迹象。 If you place the breaks at the end of each destructor, you are likely to see the corrupt stack before it actually crashes. 如果将中断放置在每个析构函数的末尾,则可能会在损坏的堆栈真正崩溃之前看到它。

If you have many objects of one type, you might remove the breakpoint for that class once you have satisfied yourself that it executes correctly. 如果您有多种类型的对象,则在确信该类可以正确执行后,可以删除该类的断点。 Also you may be able to place breakpoints only in classes that are only ever instantiated statically. 另外,您可能只能在仅静态实例化的类中放置断点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM