我在访问类变量(Qt::Action的数组)时遇到了分段错误的问题,我一直在尝试将其缩减为一个重复该问题的最小代码示例:
#!/usr/bin/ruby
require 'Qt4'
require 'rufus/scheduler'
app = Qt::Application.new(ARGV)
class ManagerWidget < Qt::Widget
signals 'changed(int)'
def initialize
super(nil)
max_index = 2
next_index = 0
scheduler = Rufus::Scheduler.start_new
scheduler.every '10s' do
emit changed(next_index)
if next_index < max_index - 1
next_index += 1
else
next_index = 0
end
end
end
end
class Tray < Qt::Widget
def initialize
super(nil)
tray = Qt::SystemTrayIcon.new
manager_widget = ManagerWidget.new
menu = Qt::Menu.new
tray_icon = Qt::Icon.new("./icons/Gnome-Preferences-Desktop-Wallpaper-64.png")
actions = []
manager_widget.connect(SIGNAL('changed(int)')) do |i|
puts "changed #{i}"
actions[i].text = '...' if actions && actions[i]
end
2.times do |i|
sub_menu = Qt::Menu.new("#{i + 1}")
actions[i] = sub_menu.add_action('x')
menu.add_menu(sub_menu)
end
tray.icon = tray_icon
tray.context_menu = menu
tray.show
end
end
Tray.new
app.exec上面的代码输出:
"sni-qt/12147" WARN 14:35:48.326 void StatusNotifierItemFactory::connectToSnw() Invalid interface to SNW_SERVICE
changed 0
changed 1
changed 0
changed 1
changed 0
changed 1
changed 0
changed 1
./min-code-example.rb:42: [BUG] Segmentation fault
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-linux]
[... snipped due to character limits on posting ...]
[NOTE]
You may have encountered a bug in the Ruby interpreter or extension libraries.
Bug reports are welcome.
For details: http://www.ruby-lang.org/bugreport.html
Aborted (core dumped)
Failed (134)似乎有必要使用子菜单来触发这个问题-至少将操作添加到任务栏菜单本身已经运行了很长一段时间,没有出现任何问题(我让它运行了超过10次,因为上面的代码出现了错误)。
Ruby版本:ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-linux]
Qt版本:4.8.1
绑定版本:qtbindings (4.8.3.0)
Linux版本:Ubuntu 12.04
另一个奇怪的行为是,puts调用的输出有时会立即显示在控制台中,其他时候只会在我将鼠标悬停在托盘图标上时打印出来。
我甚至不知道从哪里开始追踪这件事,所以你的任何意见都是非常感谢的。
更新:我已经离开这一段时间了,因为我不能再继续下去了。我试着重新开始,但我仍然遇到同样的问题。我已经更深入地研究了这个问题,因为它显示了'missing_method‘,所以我首先打印了这个类,以确保我确实在查看Qt::Action (我是),然后根据actions[0].methods来确定哪些方法是可用的。
.methods输出:
[:setShortcut, :shortcut=, :qobject_cast, :inspect, :pretty_print, :className,
:class_name, :inherits, :findChildren, :find_children, :findChild, :find_child,
:connect, :method_missing, :const_missing, :dispose, :isDisposed, :disposed?,
:qVariantValue, :qVariantFromValue, :**, :+, :~, :-@, :-, :*, :/, :%, :>>,
:<<, :&, :^, :|, :<, :<=, :>, :>=, :==, :is_a?, :kind_of?, :methods,
:protected_methods, :public_methods, :singleton_methods, :qDebug, :qFatal,
:qWarning, :SIGNAL, :SLOT, :emit, :QT_TR_NOOP, :QT_TRANSLATE_NOOP, :nil?, :===,
:=~, :!~, :eql?, :hash, :<=>, :class, :singleton_class, :clone, :dup,
:initialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust,
:untrusted?, :trust, :freeze, :frozen?, :to_s, :private_methods,
:instance_variables, :instance_variable_get, :instance_variable_set,
:instance_variable_defined?, :instance_of?, :tap, :send, :public_send,
:respond_to?, :respond_to_missing?, :extend, :display, :method, :public_method,
:define_singleton_method, :object_id, :to_enum, :enum_for, :equal?, :!, :!=,
:instance_eval, :instance_exec, :__send__, :__id__, "blockSignals", "children",
"connect", "deleteLater", "disconnect", "dumpObjectInfo", "dumpObjectTree",
"dynamicPropertyNames", "event", "eventFilter", "inherits", "installEventFilter",
"widgetType?", "killTimer", "metaObject", "moveToThread", "objectName", "parent",
"property", "qt_metacall", "qt_metacast", "removeEventFilter", "objectName=",
"parent=", "setProperty", "setUserData", "signalsBlocked", "startTimer",
"thread", "userData", "actionGroup", "activate", "associatedGraphicsWidgets",
"associatedWidgets", "autoRepeat", "data", "font", "hover", "icon", "iconText",
"checkable?", "checked?", "enabled?", "iconVisibleInMenu?", "separator?",
"visible?", "menu", "menuRole", "parentWidget", "priority", "actionGroup=",
"autoRepeat=", "checkable=", "checked=", "data=", "disabled=", "enabled=",
"font=", "icon=", "iconText=", "iconVisibleInMenu=", "menu=", "menuRole=",
"priority=", "separator=", "shortcut=", "shortcutContext=", "shortcuts=",
"softKeyRole=", "statusTip=", "text=", "toolTip=", "visible=", "whatsThis=",
"shortcut", "shortcutContext", "shortcuts", "showStatusText", "softKeyRole",
"statusTip", "text", "toggle", "toolTip", "trigger", "whatsThis"]在本例中,当分段错误发生时,我正在尝试执行actions[0].enabled = true,据我所知,这是在输出中(倒数第六行,enabled=)。我还尝试了setEnabled(true),set_enabled(true)和几乎所有我能想到的东西。
即使在操作对象上调用inspect也会导致分段错误,尽管在最初将操作放置在数组中的循环中执行此操作是可以的。我真的不知道哪里出了问题。
编辑:作为对使用QTimer的建议的回应,我已经尝试过了,但仍然遇到同样的问题。目前的代码比上面列出的代码要复杂一些,所以我将在这里编写一些快速的伪代码来说明这个流程:
def init_tray
actions = []
2.times do |i|
actions[i] = ... # init and add to systray, store reference in array
end
update = Proc.new do |i|
actions[i].setText('...') # update the text on one of the actions defined above
end
listener = ... # create a listener that can be called by Manager on certain events - will call the update Proc among other things.
manager = Manager.new(..., listener)
end对Manager.new的调用初始化该对象,并在最后调用状态更改的侦听器,而侦听器又调用访问actions数组的update进程。在这个阶段,这一切都应该在同一个线程中,除了Qt::Action的实际创建之外,这些代码行都不依赖于QT。我已经删除了Rufus调度,并将其替换为QTimer,但它还不足以成为问题所在。
发布于 2013-03-19 01:39:19
我在这个问题上花费了太多时间,却不能弄清楚为什么会发生这种情况(感谢那些试图提供帮助的人,但不幸的是,这并没有解决问题)。因此,我把我的工作放在这里,作为其他人遇到类似问题的答案:
我去掉了数组,而是将要使用它的代码包装在我在先前填充该数组的循环中创建的Proc中。由于在此循环中创建的Proc可以直接访问相关变量,因此不再需要将其存储在数组中并在以后检索它。它自己带来了一两个问题,因为Proc中的代码现在每个SIGNAL的每个操作都执行一次,而不是让一个代码块经历所有相关的操作,但它们更容易处理。
发布于 2013-03-14 03:21:37
有一件事你没有尝试过,那就是使用新版本的ruby。p0似乎充满了潜在的问题。
Ruby 1.9.3-p392是1.9.3的最新版本。
https://stackoverflow.com/questions/13399878
复制相似问题