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