[190] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
---|
| 2 | <!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/doc/debug.doc:36 -->
|
---|
| 3 | <html>
|
---|
| 4 | <head>
|
---|
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
| 6 | <title>Debugging Techniques</title>
|
---|
| 7 | <style type="text/css"><!--
|
---|
| 8 | fn { margin-left: 1cm; text-indent: -1cm; }
|
---|
| 9 | a:link { color: #004faf; text-decoration: none }
|
---|
| 10 | a:visited { color: #672967; text-decoration: none }
|
---|
| 11 | body { background: #ffffff; color: black; }
|
---|
| 12 | --></style>
|
---|
| 13 | </head>
|
---|
| 14 | <body>
|
---|
| 15 |
|
---|
| 16 | <table border="0" cellpadding="0" cellspacing="0" width="100%">
|
---|
| 17 | <tr bgcolor="#E5E5E5">
|
---|
| 18 | <td valign=center>
|
---|
| 19 | <a href="index.html">
|
---|
| 20 | <font color="#004faf">Home</font></a>
|
---|
| 21 | | <a href="classes.html">
|
---|
| 22 | <font color="#004faf">All Classes</font></a>
|
---|
| 23 | | <a href="mainclasses.html">
|
---|
| 24 | <font color="#004faf">Main Classes</font></a>
|
---|
| 25 | | <a href="annotated.html">
|
---|
| 26 | <font color="#004faf">Annotated</font></a>
|
---|
| 27 | | <a href="groups.html">
|
---|
| 28 | <font color="#004faf">Grouped Classes</font></a>
|
---|
| 29 | | <a href="functions.html">
|
---|
| 30 | <font color="#004faf">Functions</font></a>
|
---|
| 31 | </td>
|
---|
| 32 | <td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Debugging Techniques</h1>
|
---|
| 33 |
|
---|
| 34 |
|
---|
| 35 |
|
---|
| 36 | <p> Here we present some useful hints to debugging your Qt-based software.
|
---|
| 37 | <p> <h2> Command Line Options
|
---|
| 38 | </h2>
|
---|
| 39 | <a name="1"></a><p> When you run a Qt program you can specify several command line options
|
---|
| 40 | that can help with debugging.
|
---|
| 41 | <p> <center><table cellpadding="4" cellspacing="2" border="0">
|
---|
| 42 | <tr bgcolor="#a2c511"> <th valign="top">Option <th valign="top">Result
|
---|
| 43 | <tr bgcolor="#f0f0f0"> <td valign="top">-nograb
|
---|
| 44 | <td valign="top">The application should never grab <a href="qwidget.html#grabMouse">the mouse</a> or <a href="qwidget.html#grabKeyboard">the
|
---|
| 45 | keyboard</a>. This option is set by default when the
|
---|
| 46 | program is running in the <tt>gdb</tt> debugger under Linux.
|
---|
| 47 | <tr bgcolor="#d0d0d0"> <td valign="top">-dograb
|
---|
| 48 | <td valign="top">Ignore any implicit or explicit -nograb. -dograb wins over
|
---|
| 49 | -nograb even when -nograb is last on the command line.
|
---|
| 50 | <tr bgcolor="#f0f0f0"> <td valign="top">-sync
|
---|
| 51 | <td valign="top">Runs the application in X synchronous mode. Synchronous mode
|
---|
| 52 | forces the X server to perform each X client request
|
---|
| 53 | immediately and not use buffer optimization. It makes the
|
---|
| 54 | program easier to debug and often much slower. The -sync
|
---|
| 55 | option is only valid for the X11 version of Qt.
|
---|
| 56 | </table></center>
|
---|
| 57 | <p> <h2> Warning and Debugging Messages
|
---|
| 58 | </h2>
|
---|
| 59 | <a name="2"></a><p> Qt includes three global functions for writing out warning and debug
|
---|
| 60 | text.
|
---|
| 61 | <ul>
|
---|
| 62 | <li> <a href="qapplication.html#qDebug">qDebug()</a> for writing debug output for testing etc.
|
---|
| 63 | <li> <a href="qapplication.html#qWarning">qWarning()</a> for writing warning output when program
|
---|
| 64 | errors occur.
|
---|
| 65 | <li> <a href="qapplication.html#qFatal">qFatal()</a> for writing fatal error messages
|
---|
| 66 | and exiting.
|
---|
| 67 | </ul>
|
---|
| 68 | <p> The Qt implementation of these functions prints the text to the <tt>stderr</tt>
|
---|
| 69 | output under Unix/X11 and to the debugger under Windows. You can
|
---|
| 70 | take over these functions by installing a message handler;
|
---|
| 71 | <a href="qapplication.html#qInstallMsgHandler">qInstallMsgHandler()</a>.
|
---|
| 72 | <p> The debugging functions <a href="qobject.html#dumpObjectTree">QObject::dumpObjectTree</a>() and <a href="qobject.html#dumpObjectInfo">QObject::dumpObjectInfo</a>() are often useful when an application looks
|
---|
| 73 | or acts strangely. More useful if you use object names than not, but
|
---|
| 74 | often useful even without names.
|
---|
| 75 | <p> <h2> Debugging Macros
|
---|
| 76 | </h2>
|
---|
| 77 | <a name="3"></a><p> The header file <a href="qglobal-h.html">qglobal.h</a> contains many debugging macros and
|
---|
| 78 | <tt>#define</tt>s.
|
---|
| 79 | <p> Two important macros are:
|
---|
| 80 | <ul>
|
---|
| 81 | <li> <a href="qapplication.html#Q_ASSERT">Q_ASSERT(b)</a> where b is a boolean
|
---|
| 82 | expression, writes the warning: "ASSERT: 'b' in file file.cpp (234)"
|
---|
| 83 | if b is FALSE.
|
---|
| 84 | <li> <a href="qapplication.html#Q_CHECK_PTR">Q_CHECK_PTR(p)</a> where p is a pointer.
|
---|
| 85 | Writes the warning "In file file.cpp, line 234: Out of memory" if p is
|
---|
| 86 | 0.
|
---|
| 87 | </ul>
|
---|
| 88 | <p> These macros are useful for detecting program errors, e.g. like this:
|
---|
| 89 | <pre>
|
---|
| 90 | char *alloc( int size )
|
---|
| 91 | {
|
---|
| 92 | <a href="qapplication.html#Q_ASSERT">Q_ASSERT</a>( size > 0 );
|
---|
| 93 | char *p = new char[size];
|
---|
| 94 | <a href="qapplication.html#Q_CHECK_PTR">Q_CHECK_PTR</a>( p );
|
---|
| 95 | return p;
|
---|
| 96 | }
|
---|
| 97 | </pre>
|
---|
| 98 |
|
---|
| 99 | <p> If you define the flag QT_FATAL_ASSERT, Q_ASSERT will call fatal()
|
---|
| 100 | instead of warning(), so a failed assertion will cause the program to
|
---|
| 101 | exit after printing the error message.
|
---|
| 102 | <p> Note that the Q_ASSERT macro is a null expression if <tt>QT_CHECK_STATE</tt> (see
|
---|
| 103 | below) is not defined. Any code in it will simply not be
|
---|
| 104 | executed. Similarly Q_CHECK_PTR is a null expression if <tt>QT_CHECK_NULL</tt> is
|
---|
| 105 | not defined. Here is an example of how you should <em>not</em> use Q_ASSERT and
|
---|
| 106 | Q_CHECK_PTR:
|
---|
| 107 | <p> <pre>
|
---|
| 108 | char *alloc( int size )
|
---|
| 109 | {
|
---|
| 110 | char *p;
|
---|
| 111 | <a href="qapplication.html#Q_CHECK_PTR">Q_CHECK_PTR</a>( p = new char[size] ); // WRONG!
|
---|
| 112 | return p;
|
---|
| 113 | }
|
---|
| 114 | </pre>
|
---|
| 115 |
|
---|
| 116 | <p> The problem is tricky: <em>p</em> is set to a sane value only as long as the
|
---|
| 117 | correct checking flags are defined. If this code is compiled without
|
---|
| 118 | the QT_CHECK_NULL flag defined, the code in the Q_CHECK_PTR expression is
|
---|
| 119 | not executed (correctly, since it's only a debugging aid) and <em>alloc</em>
|
---|
| 120 | returns a wild pointer.
|
---|
| 121 | <p> The Qt library contains hundreds of internal checks that will print
|
---|
| 122 | warning messages when some error is detected.
|
---|
| 123 | <p> The tests for sanity and the resulting warning messages inside Qt are
|
---|
| 124 | conditional, based on the state of various debugging flags:
|
---|
| 125 | <center><table cellpadding="4" cellspacing="2" border="0">
|
---|
| 126 | <tr bgcolor="#a2c511"> <th valign="top">Flag <th valign="top">Meaning
|
---|
| 127 | <tr bgcolor="#d0d0d0"> <td valign="top">QT_CHECK_STATE <td valign="top">Check for consistent/expected object state
|
---|
| 128 | <tr bgcolor="#f0f0f0"> <td valign="top">QT_CHECK_RANGE <td valign="top">Check for variable range errors
|
---|
| 129 | <tr bgcolor="#d0d0d0"> <td valign="top">QT_CHECK_NULL <td valign="top">Check for dangerous null pointers
|
---|
| 130 | <tr bgcolor="#f0f0f0"> <td valign="top">QT_CHECK_MATH <td valign="top">Check for dangerous math, e.g. division by 0
|
---|
| 131 | <tr bgcolor="#d0d0d0"> <td valign="top">QT_NO_CHECK <td valign="top">Turn off all QT_CHECK_... flags
|
---|
| 132 | <tr bgcolor="#f0f0f0"> <td valign="top">QT_DEBUG <td valign="top">Enable debugging code
|
---|
| 133 | <tr bgcolor="#d0d0d0"> <td valign="top">QT_NO_DEBUG <td valign="top">Turn off QT_DEBUG flag
|
---|
| 134 | </table></center>
|
---|
| 135 | <p> By default, both QT_DEBUG and all the QT_CHECK flags are on. To turn
|
---|
| 136 | off QT_DEBUG, define QT_NO_DEBUG. To turn off the QT_CHECK flags,
|
---|
| 137 | define QT_NO_CHECK.
|
---|
| 138 | <p> Example:
|
---|
| 139 | <pre>
|
---|
| 140 | void f( char *p, int i )
|
---|
| 141 | {
|
---|
| 142 | #if defined(QT_CHECK_NULL)
|
---|
| 143 | if ( p == 0 )
|
---|
| 144 | <a href="qapplication.html#qWarning">qWarning</a>( "f: Null pointer not allowed" );
|
---|
| 145 | #endif
|
---|
| 146 |
|
---|
| 147 | #if defined(QT_CHECK_RANGE)
|
---|
| 148 | if ( i < 0 )
|
---|
| 149 | <a href="qapplication.html#qWarning">qWarning</a>( "f: The index cannot be negative" );
|
---|
| 150 | #endif
|
---|
| 151 | }
|
---|
| 152 | </pre>
|
---|
| 153 |
|
---|
| 154 | <p> <h2> Common bugs
|
---|
| 155 | </h2>
|
---|
| 156 | <a name="4"></a><p> There is one bug that is so common that it deserves mention here: If
|
---|
| 157 | you include the <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a> macro in a class declaration and run the
|
---|
| 158 | <a href="moc.html">moc</a>, but forget to link the <a href="moc.html#moc">moc</a>-generated
|
---|
| 159 | object code into your executable, you will get very confusing error
|
---|
| 160 | messages. Any link error complaining about a lack of <tt>vtbl</tt>,
|
---|
| 161 | <tt>_vtbl</tt>, <tt>__vtbl</tt> or similar is likely to be a result of this
|
---|
| 162 | problem.
|
---|
| 163 | <p>
|
---|
| 164 | <!-- eof -->
|
---|
| 165 | <p><address><hr><div align=center>
|
---|
| 166 | <table width=100% cellspacing=0 border=0><tr>
|
---|
| 167 | <td>Copyright © 2007
|
---|
| 168 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
| 169 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
| 170 | </table></div></address></body>
|
---|
| 171 | </html>
|
---|