Segmentation fault: QApplication::~QApplication() is being called twice

edited July 2015 in Ruby Scripting

I am getting seg faults in my unit tests. I have traced the problem to the QApplication destructor being called twice, and I believe the resulting seg fault is from an attempt to free already freed memeory.

Having set a breakpoint in the destructor, here are the tracebacks for the first and second times the destructor is called:

#0  0x00002aaaab2eb540 in QApplication::~QApplication() () from /usr/pack/qt-4.8.5-di/x86_64-Linux-2.6/lib/libQtGui.so.4
#1  0x00002aaab29b5131 in __smokeqtgui::x_QApplication::~x_QApplication() () from /usr/pack/qt4-qtruby-2.2.0-di/x86_64-Linux-2.6/lib/libsmokeqtgui.so.3
#2  0x00002aaab29b5182 in __smokeqtgui::x_QApplication::~x_QApplication() () from /usr/pack/qt4-qtruby-2.2.0-di/x86_64-Linux-2.6/lib/libsmokeqtgui.so.3
#3  0x00002aaab29abf75 in __smokeqtgui::xcall_QApplication(short, void*, Smoke::StackItem*) () from /usr/pack/qt4-qtruby-2.2.0-di/x86_64-Linux-2.6/lib/libsmokeqtgui.so.3
#4  0x00002aaab42e7d3b in smokeruby_free(void*) () from /usr/pack/qt4-qtruby-2.2.0-di/x86_64-Linux-2.6/lib/libqtruby4shared.so.2
#5  0x00002aaaaab2c5fd in run_final (objspace=0xc1aa1d0, obj=250583880) at gc.c:2932
#6  0x00002aaaaab29f11 in finalize_list (objspace=0xc1aa1d0, p=0xeef9b48) at gc.c:1946
#7  0x00002aaaaab2cacd in rb_objspace_call_finalizer (objspace=0xc1aa1d0) at gc.c:3064
#8  0x00002aaaaab2c79f in rb_gc_call_finalizer_at_exit () at gc.c:2994
#9  0x00002aaaaab16219 in ruby_finalize_1 () at eval.c:101
#10 0x00002aaaaab16229 in ruby_finalize () at eval.c:108
#11 0x0000000002c98133 in rba::RubyInterpreter::~RubyInterpreter() ()
#12 0x0000000002c981a9 in rba::RubyInterpreter::~RubyInterpreter() ()
#13 0x0000000002b3a13f in lay::Application::shutdown() ()
#14 0x0000000002b3c4bc in lay::Application::~Application() ()
#15 0x0000000002058e46 in klayout_main(int, char**) ()
#16 0x0000000002059434 in main ()

Second traceback:

#0  0x00002aaaab2eb540 in QApplication::~QApplication() () from /usr/pack/qt-4.8.5-di/x86_64-Linux-2.6/lib/libQtGui.so.4
#1  0x0000000002b3c8bd in lay::Application::~Application() ()
#2  0x0000000002058e46 in klayout_main(int, char**) ()
#3  0x0000000002059434 in main ()

The traces don't show my own Ruby code, so I don't know how to make my code cause only a single call to the destructor. Is this a bug? Or is there something I can do to cause just a single call?

I am using Klayout 0.24 python eval, Qt 4.8.5, Ruby 1.9.3, RH5 Linux.

Thanks,
Dave

Comments

  • edited November -1

    In order to not get this message in my unit tests:

    Fatal: QWidget: Cannot create a QWidget when no GUI is being used
    

    I use this at the beginning of my unit tests:

    $qAPP ||= Qt::Application.new([])
    

    $qAPP is apparently used by qtruby to hold the instance of the application. If Application::shutdown were to query the Ruby interpreter if $qApp is defined, it might be able to avoid a second call to QApplication::~QApplication().

    Alternatively, I could change my invocation of Klayout from:

    klayout -b -rd no_autorun=true -rm test/test_sometest.rb

    To:

    klayout -z -rx -rd no_autorun=true -rm test/test_sometest.rb

    and remove the definition of $qApp from the test, but this will load more of KLayout than I need or want.

    Thanks,
    Dave

  • edited November -1

    Hi Dave,

    I think you already guessed right that qApp is interfering with KLayout's internal QCoreApplication object. Before the ruby interpreter is shut down, KLayout will delete QApplication instance and that makes $qApp being deleted too.

    Just a question: What do you mean by "loads more than I need or want" for the second call option? If you add "-nc" KLayout won't even load the configuration file. Then there is no big difference between "-b" and "-z -rx -nc" apart from the QApplication object which you need to run the UI tests. This means you would not need a separate QApplication object.

    Another (ugly) option is to do a hard shutdown using "exit!" at the end of your script. As far as I know that will make the application not execute the shutdown code.

    Matthias

  • edited July 2015

    Matthias,

    By "loads more than I need or want" I meant that I don't want to load anything but the minimum I need to run a test so as to minimize variables outside my control. Since I don't know precisely what gets included/excluded by the flags, I just tried to use those that removed as much as possible. This is really just me operating from a position of ignorance.

    I will change over to '-z -rx -nc' flags for running my tests. Thanks for the clarification.

    Dave

Sign In or Register to comment.