Subject: Qt4 problems
To: None <netbsd-users@netbsd.org>
From: Matthew Fincham <matthewf@cat.co.za>
List: netbsd-users
Date: 12/19/2006 13:59:43
Hi

Please bear with me if this is posted to the wrong group - this seems like
the best starting point. Also, this is a fairly long posting, so be warned.

I am having trouble with Qt4, although I don't think it is specific to Qt4.
To illustrate I will take one of the Qt4 examples (widgets/groupbox) and
make modifications to it.

----------------------------------------------------------------------
Original example:

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    Window window;
    window.show();
    return app.exec();
}

This program works correctly.


----------------------------------------------------------------------
Modification 1:

0026  class Kernel : public QApplication
0027  {
0028  public:
0029   Kernel(int argc, char** argv) : QApplication(argc,argv) {}
0030   virtual ~Kernel() {}
0031  };
0032
0033  int main(int argc, char *argv[])
0034  {
0035      Kernel app(argc, argv);
0036
0037      Window window;
0038      window.show();
0039      return app.exec();
0040  }

Here a class is descended from QApplication. This program crashes with the
following backtrace:

(gdb) bt
#0  0x48bbd1f3 in QString::fromLocal8Bit (str=0x3 <Address 0x3 out of
bounds>, size=-1)
    at tools/qstring.cpp:3291
#1  0x48c3b28b in QCoreApplication::arguments () at
kernel/qcoreapplication.cpp:1616

[ snip ]

#22 0x48c368b9 in QEventLoop::processEvents (this=0xbfbff710,
flags=0xbfbff6a0) at kernel/qeventloop.cpp:124
#23 0x48c369a3 in QEventLoop::exec (this=0xbfbff710, flags=0xbfbff6f0) at
kernel/qeventloop.cpp:170
#24 0x48c39366 in QCoreApplication::exec () at
kernel/qcoreapplication.cpp:725
#25 0x48285385 in QApplication::exec () at kernel/qapplication.cpp:2925
#26 0x0804c873 in main (argc=1, argv=0xbfbff808) at main.cpp:39
(gdb)


The arguments returned by QCoreApplication are invalid. The arguments are
obtained from a member variable of QCoreApplication. Watching for where this
value changes looks as follows:

(gdb) b main.cpp:35
Breakpoint 1 at 0x804c839: file main.cpp, line 35.
(gdb) run
Starting program:
/usr/local/qt-x11-commercial-src-4.2.2/examples/widgets/groupbox/groupbox

Breakpoint 1, main (argc=1, argv=0xbfbff7c4) at main.cpp:35
35              MyKernel app(argc, argv);
(gdb) n
37              Window window;
(gdb) p ((QCoreApplicationPrivate*)(QCoreApplication::self->d_ptr))->argc
$1 = (int &) @0xbfbff704: 1
(gdb) watch *((int*)0xbfbff704)
Watchpoint 2: *(int *) 3217028868
(gdb) continu
Continuing.
Watchpoint 2: *(int *) 3217028868

Old value = 1
New value = 0
0x0804c853 in main (argc=1, argv=0xbfbff7c4) at main.cpp:37
37              Window window;
(gdb) bt
#0  0x0804c853 in main (argc=1, argv=0xbfbff7c4) at main.cpp:37
(gdb)

So the value of argc change after the construction of MyKernel, but before
the construction of Window (!!?), with the backtrace revealing very little.

----------------------------------------------------------------------
Modification 2:

I have simplified this example further by removing the Window class:

0026  QApplication* create(int argc, char *argv[])
0027  {
0028   QApplication* app=new QApplication(argc,argv);
0029  return app;
0030  }
0031
0032  int dummyFunction(int i)
0033  {
0034  return i;
0035  }
0036
0037  int main(int argc, char *argv[])
0038  {
0039  QApplication* app=create(argc, argv);
0040
0041  int a=10;
0042  int b=dummyFunction(a);
0043  a=b;
0044
0045  return 0;
0046  }

This example exhibits the same problem - the arguments become invalid.
Running with gdb to find the point at which the value changes looks as
follows:

(gdb) b main.cpp:41
Breakpoint 1 at 0x804c3c0: file main.cpp, line 41.
(gdb) run
Starting program:
/usr/local/qt-x11-commercial-src-4.2.2/examples/widgets/groupbox/groupbox

Breakpoint 1, main (argc=1, argv=0xbfbff7c4) at main.cpp:41
41              int a=10;
(gdb) p ((QCoreApplicationPrivate*)(QCoreApplication::self->d_ptr))->argc
$1 = (int &) @0xbfbff730: 1
(gdb) watch *((int*)0xbfbff730)
Watchpoint 2: *(int *) 3217028912
(gdb) x 0xbfbff730
0xbfbff730:     0x00000001
(gdb) contin
Continuing.
Watchpoint 2: *(int *) 3217028912

Old value = 1
New value = 10
0x0804c3cd in main (argc=1, argv=0xbfbff7c4) at main.cpp:42
42              int b=dummyFunction(a);
(gdb) bt
#0  0x0804c3cd in main (argc=1, argv=0xbfbff7c4) at main.cpp:42
(gdb)


So here we see the value changes after the create function, but before the
call to dummyFunction.

These results were obtained using:
NetBSD 2.0_STABLE
gcc 3.3.3
gdb 6.3
Qt 4.2.2

Similar results have been obtained on NetBSD 3.0_STABLE. I also upgraded the
compiler to 4.1.1 and had similar problems.

I would appreciate any tips on further debugging this, or a redirection for
my query.


Many thanks
Matthew Fincham