[Kde-bindings] Crash in qtruby4 (svn) using custom C++ classes

Stefano Crocco stefano.crocco at alice.it
Fri Jun 20 12:46:08 UTC 2008


> On Thursday 19 June 2008, Stefano Crocco wrote:
> > I'm attempting to use the latest svn revision of korundum4 to build an
> > application which uses a custom C++ class, following the procedure
> > explained in a thread on the korundum forum
> > (http://rubyforge.org/forum/forum.php?thread_id=10371&forum_id=723). The
> > code I wrote worked correctly with an older revision (from about a month
> > ago), but now it crashes when I try to write a C++ slot which returns a
> > string.
> >
> > Here's a sample code which causes the crash:
> >
> > #include <ruby.h>
> > #include <QApplication>
> > #include <QList>
> > #include <QPoint>
> > #include <QWidget>
> >
> > class MyWidget: public QWidget{
> >   Q_OBJECT
> >
> >   public:
> >   MyWidget(QWidget*parent = 0):QWidget(parent){
> >   }
> >   ~MyWidget(){}
> >
> >   public slots:
> >   QString string_test(){return QString("My test");}
> >   int int_test(){return 124;}
> >   QPoint point_test(){return QPoint(12,89);}
> > };
> >
> > #include "ext_moc.cpp"
> >
> > static VALUE create_my_widget(VALUE self, VALUE name){
> >   QWidgetList l = QApplication::allWidgets();
> >   QWidget * parent = l.first();
> >   MyWidget * w = new MyWidget(parent);
> >   QString objName = STR2CSTR(name);
> >   w->setObjectName(objName);
> >   return Qnil;
> > }
> >
> > extern "C" void Init_ext(void){
> >   typedef VALUE ( *HOOK ) ( ... );
> >   rb_define_method(rb_cObject, "create_my_widget",
> > reinterpret_cast<HOOK>(create_my_widget), 1); }
> >
> > Once compiled that as a ruby extension file called ext.so, if I run the
> > following ruby code:
> >
> > require 'Qt4'
> > require 'ext'
> >
> > app = Qt::Application.new []
> > w = Qt::Widget.new
> > create_my_widget "test"
> > my_widget = w.findChild Qt::Object, "test"
> > puts "int_test:#{my_widget.int_test}"
> > puts "point_test: #{my_widget.point_test.inspect}"
> > puts "doing string_test"
> > p my_widget.string_test
> >
> > This is what I get:
> > int_test:124
> > point_test: nil
> > doing string_test
> > try.rb:11: [BUG] Segmentation fault
> > ruby 1.8.6 (2008-03-03) [i686-linux]
> >
> > And here's the backtrace obtained by running ruby in gdb:
> > #0  0xb7afc222 in QString::operator= () from /usr/lib/qt4/libQtCore.so.4
> > #1  0xb6b3af73 in MyWidget::qt_metacall () from ./ext.so
> > #2  0xb756356e in InvokeNativeSlot::invokeSlot (this=0xbfe447f4) at
> > /home/stefano/Desktop/kdebindings/ruby/qtruby/src/Qt.cpp:413 #3 
> > 0xb756366d in InvokeNativeSlot::mainfunction (this=0xbfe447f4) at
> > /home/stefano/Desktop/kdebindings/ruby/qtruby/src/Qt.cpp:424 #4 
> > 0xb759c96c in SigSlotBase::next (this=0xbfe447f4) at
> > /home/stefano/Desktop/kdebindings/ruby/qtruby/src/marshall_types.cpp:628
> > #5 0xb7568d19 in method_missing (argc=1, argv=0xbfe44bc0,
> > self=3060248180) at
> > /home/stefano/Desktop/kdebindings/ruby/qtruby/src/Qt.cpp:856 #6 
> > 0xb7f08805 in call_cfunc () from /usr/lib/libruby18.so.1.8
> > #7  0xb7f12945 in rb_call0 () from /usr/lib/libruby18.so.1.8
> > #8  0xb7f12ad1 in rb_call () from /usr/lib/libruby18.so.1.8
> > #9  0xb7f133ad in rb_funcall2 () from /usr/lib/libruby18.so.1.8
> > #10 0xb7f18b36 in method_missing () from /usr/lib/libruby18.so.1.8
> > #11 0xb7f12bc3 in rb_call () from /usr/lib/libruby18.so.1.8
> > #12 0xb7f0d599 in rb_eval () from /usr/lib/libruby18.so.1.8
> > #13 0xb7f0fcc5 in rb_eval () from /usr/lib/libruby18.so.1.8
> > #14 0xb7f1dfe3 in ruby_exec_internal () from /usr/lib/libruby18.so.1.8
> > #15 0xb7f1e023 in ruby_exec () from /usr/lib/libruby18.so.1.8
> > #16 0xb7f1e058 in ruby_run () from /usr/lib/libruby18.so.1.8
> > #17 0x08048715 in main ()
> >
> > Stefano
> I've just tried it, and it works for me:
>
> mardigras rdale 961% ruby try.rb
> int_test:124
> point_test: nil
> doing string_test
> "My test"
>
> But point_test() shouldn't be returning nil, and so there is some sort of
> bug with that. I'll see if I can find what is causing that, as it used to
> be possible to return things like QPoints.
>
> I'm not sure why you're getting a crash and I'm not.
>
> -- Richard

First of all, sorry if this mail won't show at the correct place in the 
thread. I never received Richard's answer (I read it in the archives), so I 
couln't respond to it.

I don't know what to say, either. Can it be caused by something wrong with my
compiler/linker? I'm using gcc 4.3.1 and ld 2.18.

Maybe there is some issue with the commands I use to generate the extension 
(I'm not very familiar with the gcc options). They are:

moc -o ext_moc.cpp ext.cpp

i686-pc-linux-gnu-g++ -I. -I/usr/lib/ruby/1.8/i686-linux \
-I/usr/lib/ruby/1.8/i686-linux -I.  -march=pentium4 \
-c ext.cpp -I/usr/include/qt4/QtGui -I/usr/include/qt4/QtCore \
-I/usr/include/qt4/Qt -I/usr/include/qt4 \
-I/usr/kde/svn/include/KDE -I/usr/kde/svn/include/

i686-pc-linux-gnu-g++ -shared -o ext.so ext.o -L'.' -L'/usr/lib' -Wl,\
-R'/usr/lib' -L. -rdynamic -Wl,-export-dynamic\ -Wl -Wl -L/usr/lib -lruby18 \
-lpthread -ldl -lcrypt -lm  -lc -L/usr/lib/qt4 -lQtCore -lQtGui -lkdeui \
-lkdecore -lkparts -lktexteditor -L/usr/kde/svn/lib

I use rake as build tool, since I couldn't get mkfm to correctly handle the 
cpp file generated by moc (it insists in wanting to compile it by itself 
instead of including it in the other .cpp file)

Is there a way I can get more accurate debugging information?

Stefano




More information about the Kde-bindings mailing list