简体   繁体   English

C ++中的SIGSEGV崩溃预防

[英]SIGSEGV Crash prevention in C++

What is wrong in preventing SIGSEGV crashes by putting the body of main inside a __try{} __catch{} block? 通过将main主体放在__try {} __catch {}块中来防止SIGSEGV崩溃是怎么回事? Anyway nothing bad would have happened since the OS would have prevented it, and I would have just failed in whatever operation I tried to perform. 无论如何,因为操作系统可以阻止它,所以不会发生任何不好的事情,而我尝试执行的任何操作都将失败。 I agree this is not the correct thing to do. 我同意这不是正确的做法。 But what are problems other than that I might fail some operations? 但是除了我可能无法进行某些操作之外,还有什么问题?

What is wrong in preventing SIGSEGV crashes by putting the body of main inside a __try{} __catch{} block? 通过将main主体放在__try {} __catch {}块中来防止SIGSEGV崩溃是怎么回事?

It is probably not guaranteed to fix the problem. 可能不能保证解决此问题。 Most things that result in SIGSEGV are caused by undefined behaviour. 导致SIGSEGV的大多数情况是由不确定的行为引起的。 When the behaviour of the program is undefined, it is not guaranteed to receive a SIGSEGV, nor is it guaranteed to behave as you would want. 当程序的行为未定义时,不能保证它会收到SIGSEGV,也不能保证其行为符合您的期望。 Any behaviour is possible. 任何行为都是可能的。 In that situation the safest thing to do is often to terminate the process. 在这种情况下,最安全的做法通常是终止该过程。

Using __try{} __catch{} just in case is not by itself necessarily wrong, but do note that this is a compiler and system specific feature and so you need to take additional steps to keep your program portable to other compilers and systems if you care about that. 以防万一本身不一定是错误的,请使用__try{} __catch{} ,但请注意,这是编译器和系统的特定功能,因此,如果需要,您需要采取其他步骤使程序可移植到其他编译器和系统中关于那个。

Furthermore, letting the program crash usually has the advantage that the operating system can generate a core dump for you, which can be used to debug the program to possibly find out the cause of the crash. 此外,让程序崩溃通常具有以下优点:操作系统可以为您生成一个核心转储,该核心转储可用于调试程序以找出崩溃的原因。 I'm not intimately familiar with __try{} __catch{} but if it unwinds the stack like the regular catch does, then the call trace information is lost, potentially making debugging harder. 我对__try{} __catch{}并不是很熟悉,但是如果它像常规catch那样展开堆栈,则会丢失呼叫跟踪信息,从而可能使调试更加困难。

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

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