問題描述
最近在我的一個程序中,我遇到了分段錯誤問題.我設法找到了導致問題的線路,但我還沒有找到解決方法.
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
這篇關于使用“嘗試";避免分段錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!