使用“尝试";避免分段错误
问题描述
最近在我的一个程序中,我遇到了分段错误问题.我设法找到了导致问题的线路,但我还没有找到解决方法.
Recently in one of my programs I got a segmentation fault problem. I managed to find the line that its is causing the problem but I haven't find the way of fixing it.
行:
self.window_player.add(self.vlc)
其中 self.vlc
是一个小部件,self.window_player
是一个空的 Gtk.Window()
在 glade 中创建.
where self.vlc
is a widget and self.window_player
is an empty Gtk.Window()
created in glade.
该行在我的程序的 __init__
处,所以实际上这个问题只在启动程序时发生.奇怪的事实是,错误只出现了 10 次中的 1 次(启动程序)
The line is at the __init__
of my program, so actually this problem only happen when launching the program. The weird fact is that the error only appears like 1 of 10 times (of launching the program)
错误:Segmentation fault
是我从终端得到的唯一输出
the error:
Segmentation fault
is the only output that I get from the terminal
所以我尝试了:
while True:
try:
self.window_player.add(self.vlc)
break
except:
print "segmentation"
问题是try
似乎没有排除分段错误!
The problem is that the segmentation fault don't seems to be excepted by the try
!
解决方案
对不起,你处理不了.段错误是由内存损坏、超出自有内存边界的读取或写入、双重释放和其他一些原因引起的.
Sorry, you can't handle it. A segfault is caused by memory corruption, reading or writing beyond boundaries of owned memory, double frees, and a few other.
您可以在此处找到一些导致段错误的问题示例:
You can found a few examples of issues that cause a segfault here:
https://gist.github.com/carlos-jenkins/8873932
操作系统会杀死有问题的程序,你无能为力.您唯一的解决方案是纠正根本问题.
The operating system will kill the offending program, you can't do much about it. Your only solution is to correct the root problem.
您可以使用 Valgrind 工具运行程序,它可以让您准确找到问题所在:
You can run a program using the tool Valgrind, it will allow you to find exactly where the problem is:
http://valgrind.org/
在 Ubuntu 上,只需 sudo apt-get install valgrind
然后 valgrind <program cmd>
将启动程序.这种偏离会慢很多,但大多数时候会发现问题.
On Ubuntu, just sudo apt-get install valgrind
and then valgrind <program cmd>
will launch the program. This offcourse will be a lot slower, but will identify the problem most of the time.
旁注:从技术上讲,您可以通过为该信号注册回调来捕获 SIGSEV 信号.但你不应该.有关更多信息,请参阅此答案:
Side note: Technically you can catch a SIGSEV signal by registering a callback for this signal. But you shouldn't. See this answer for more information:
https://stackoverflow.com/a/10203062/439494
相关文章