Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Classes  ·  Annotées  ·  Hiérarchie  ·  Fonctions  ·  Structure  · 

Debugging Techniques


Here we present some useful hints to debugging your Qt-based software.

Command Line Options

When you run a Qt program you can specify several command line options that can help with debugging.

  • -nograb The application should never grab the mouse or the keyboard. This option is set by default when the program is running in the gdb debugger under Linux.
  • -dograb Ignore any implicit or explicit -nograb. -dograb wins over -nograb even when -nograb is last on the command line.
  • -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.


Warning and Debugging Messages

Qt includes three global functions for writing out warning and debug text.

  • qDebug() for writing debug output for testing etc.
  • qWarning() for writing warning output when program errors occur.
  • qFatal() for writing fatal error messages and exit.

The Qt implementation of these functions prints the text to the stderr output under Unix/X11 and to the debugger under Windows. You can take over these functions by installing a message handler; qInstallMsgHandler().

The debugging functions QObject::dumpObjectTree() and QObject::dumpObjectInfo() 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.


Debugging Macros

The header file qglobal.h contains many debugging macros and #defines.

Two important macros are:

  • ASSERT(b) where b is a boolean expression, writes the warning: "ASSERT: 'b' in file file.cpp (234)" if b is FALSE.
  • CHECK_PTR(p) where p is a pointer. Writes the warning "In file file.cpp, line 234: Out of memory" if p is null.

These macros are useful for detecting program errors, e.g. like this:

  char *alloc( int size )
  {
      ASSERT( size > 0 );
      char *p = new char[size];
      CHECK_PTR( p );
      return 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.

Note that the ASSERT macro is a null expression if CHECK_STATE (see below) is not defined. Any code in it will simply not be executed. Similarly CHECK_PTR is a null expression if CHECK_NULL is not defined. Here is an example of how you should NOT use ASSERT and CHECK_PTR:

  char *alloc( int size )
  {
      char *p;
      CHECK_PTR( p = new char[size] );  // never do this!
      return p;
  }

The problem is tricky: p 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 alloc returns a wild pointer.

The Qt library contains hundreds of internal checks that will print warning messages when some error is detected.

The tests for sanity and the resulting warning messages inside Qt are conditional, based on the state of various debugging flags:

  • CHECK_STATE: Check for consistent/expected object state
  • CHECK_RANGE: Check for variables range errors
  • CHECK_NULL: Check for dangerous null pointer
  • CHECK_MATH: Check for dangerous math, e.g. division by 0.
  • NO_CHECK: Turn off all CHECK_... flags
  • DEBUG: Enable debugging code
  • NO_DEBUG: Turn off DEBUG flag

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.

Example:

  void f( char *p, int i )
  {
  #if defined(CHECK_NULL)
      if ( p == 0 )
          qWarning( "f: Null pointer not allowed" );
  #endif

  #if defined(CHECK_RANGE)
      if ( i < 0 )
          qWarning( "f: The index cannot be negative" );
  #endif
  }


Common bugs

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.

Any link error complaining about a lack of vtbl, _vtbl, __vtbl or similar is likely to be this problem.

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 64
  2. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  3. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  4. BlackBerry 10 : premières images du prochain OS de RIM qui devrait intégrer des widgets et des tuiles inspirées de Windows Phone 0
  5. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  6. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
  7. La rubrique Qt a besoin de vous ! 1
Page suivante

Le blog Digia au hasard

Logo

Déploiement d'applications Qt Commercial sur les tablettes Windows 8

Le blog Digia est l'endroit privilégié pour la communication sur l'édition commerciale de Qt, où des réponses publiques sont apportées aux questions les plus posées au support. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 2.3
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web