一致的方式来捕获C ++库崩溃 [英] Consistant Way to Catch C++ Library Crashes

查看:239
本文介绍了一致的方式来捕获C ++库崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在不同的网站环顾四周,找不到这个问题的答案,除了那些似乎不会工作的人。正如标题所说,我试图找到一种方法来捕获如果我的图书馆工作崩溃。我有一个Root类,它持有我在库中的许多经理样式类的实例,它释放它的析构函数中的实例。自然,管理者负责相当多的数据,所以如果他们没有被妥善处理,有一个机会,一个相当不可接受的,甚至危险的内存泄漏级别。



我知道,当程序崩溃时,操作系统将释放程序的堆栈空间,但不包括调用分配的对象的析构函数。它也不包括删除在执行期间分配的任何堆,这是我在我的库中处理大量的内存,回到广泛的内存泄漏的问题。



我在其他网站上遇到的很多答案只是说要用 atexit()注册一个函数,但该函数不会工作如果应用程序崩溃。并且,如上所述,由于崩溃不调用析构函数,没有办法使一个全局单例,当它被销毁时,关闭一切,这是我最初的想法如何处理这一点。我的另一个想法是只希望图书馆的最终用户采取适当的预防措施,以避免崩溃(通过广泛使用异常抛出),但我觉得违反了一个良好的图书馆的想法,坦白地说,我认为它要求一些最终用户来处理。



所以我想我的TL; DR问题是:有一种方法,通过一个标准的C ++函数,或通过某种类型的管理器类,当库崩溃并适当处理时捕获?



编辑:此外,我真的更喜欢一个跨平台的方式处理这个。我的代码库大量使用C ++ 11的功能,所以我已经编程限制可用的编译器到最新版本的GCC和Clang。



不仅如此,我也有几个类,像一个Logger,将关闭他们的流到文件系统,并打印出一些关于退出状态的消息。我也有一个内存跟踪器,它报告任何可能的内存泄漏到文件,但只在它的析构函数。

解决方案

b

有没有办法,通过标准的C ++函数,或通过某种类型的管理器类,当库崩溃并适当处理它时捕获?

对于你的问题,最简洁的答案我可以想象是:


坚持 C ++标准错误处理 的类和类别, a>。







由于您要求 atexit ),该行为在标准参考



注意还有其他处理程序机制,例如 std :: terminate_handler ,允许您以便携式和标准兼容的方式处理某些异常中止情况。 / p>

最后但并非最不重要的是,可能需要安装某些(操作系统特定)信号处理程序,以捕获像所谓的分段错误 (SIGSEV)

$

$ b

I've looked around at different sites, and cannot find any answer to this question, other than ones that don't seem that they would work. As the title says, I am trying to find a way to catch if the library I am working on crashes. I have a Root class that holds instances of the many manager-style classes that I have in my library, and it releases the instances in it's destructor. Naturally, the managers are responsible for quite a bit of data, so if they are not properly disposed of there is a chance for a rather unacceptable, even dangerous level of memory leaks.

I am aware that, when a program crashes, that the OS will deallocate the stack space for the program, but that does not include calling the destructors of the allocated objects. It also does not include deleting any of the heap allocated during the execution, which is how I am dealing with a good deal of the memory in my library, which goes back to problem of widespread memory leaks.

A good deal of the answers I am coming across on other sites are just saying to register a function with atexit(), but that function does not work if the application crashes. And, as stated above, since crashes do not call destructors, there is no way to make a global singleton that closes everything when it is destroyed, which was my initial idea of how to deal with this. My other idea was to just hope that the end user of the library will take the proper precautions to avoid crashes (through widespread use of exception throwing), but I feel that goes against the idea of a well-coded library, and frankly I think it's asking a bit much of the end user to deal with that.

So I guess my TL;DR question is this: Is there a way, either through a standard C++ function, or through some sort of manager class, to catch when a library crashes and deal with it appropriately?

EDIT: Also, I would really prefer a cross platform way to deal with this. My code base heavily uses features of C++11, so I've programatically limited the useable compilers to the latest versions of GCC and Clang.

Not only that, but I also have a couple of classes, like a Logger, that will close their stream to the filesystem and print out a couple of messages about the exit status. I also have a memory tracker that reports any possible memory leaks to a file, but only in it's destructor.

解决方案

"Is there a way, either through a standard C++ function, or through some sort of manager class, to catch when a library crashes and deal with it appropriately?"

The most concise answer for your question I can imagine is:

Stick with the classes and categories of the C++ Standard Error handling.


As you are asking for atexit(), the behaviour is well defined in the standard reference as well.

Note there are further handler mechanisms like the std::terminate_handler, that allow you to handle some exceptional abort situations, in a portable and standard compliant way.

Last but not least it might be necessary to install certain (OS specific) signal handlers, to catch up errors like a so called segmentation fault (SIGSEV) raised due to stack overflow or similar things.

这篇关于一致的方式来捕获C ++库崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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