using quot;tryquot; to avoiding a segmentation fault(使用“尝试;避免分段错误)
问题描述
最近在我的一个程序中,我遇到了分段错误问题.我设法找到了导致问题的线路,但我还没有找到解决方法.
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
这篇关于使用“尝试";避免分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用“尝试";避免分段错误
基础教程推荐
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
