Qt debugging technology

Source: Internet
Author: User

Qt debugging technology

Here we mention the use of some QT program debugging.

I. command line options
When you run the QT program, you can specify several command line options to help debug the program.

-The nograb application does not snatch the mouse or keyboard. This value is set by default when the program runs under the gdb debugging tool in Linux.
-Dograb ignores any implied or express-nograb. Even if-nograb is placed at the end of the command line,-dograb has a higher priority than-nograb.
-The sync application runs in synchronization mode of X. in synchronization mode, the X server is forced to immediately execute requests from each X client without the optimization of the buffer. it makes the program easier to debug, but the speed is slower. -The sync option is only valid in QT X11.

Ii. Warning and debugging information
Qt has this global function to output warning and debugging text.

Qdebug () is used for testing and other debugging information output.
Qwarning () is used to output warning information when an error occurs in the program.
Qfatal () is used to output devastating error messages and exit the program.
Qt passes the information to stderr output in UNIX/X11 or to the debugger in windows through the execution of these functions. you can use qinstallmsghandler () to install message handler to take over these functions.

When an application runs strangely, it is useful to debug the functions qobject: dumpobjecttree () and qobject: dumpobjectinfo. object names are more helpful than object names.

Iii. debugging macros
The header file qglobal. h contains many debugging macros and their definitions.

Two important macros:

Assert (B) Where B is a Boolean expression. If B is false, a warning message is output: "assert: 'B' in file. CPP (234) "----> indicates the file. the value of B in row 234th of CPP is false.
Check_ptr (p) where p is a pointer. when P is null, the warning message "in file. CPP, line 234: out of memory "----> indicates the file. 234th rows of CPP, memory overflow.
These macros are very useful when detecting program errors, such:

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 () to replace warning (). Therefore, a failed assert will cause the program to exit after an error message is output.
Note: If the check_state (as described below) is not defined, the assert macro is an empty expression. the code in it will not be executed. similarly, if check_null does not have a chameleon, check_ptr is also an empty expression. here is an example of how to make assert or check_ptr not:

Char * alloc (INT size)
{
Char * P;
Check_ptr (P = new char [size]); // never do this!
Return P;
}

This program is complicated: only when the correct test mark is defined will p be set to a stable value. if the Code does not define the check_null flag, the code in the check_ptr expression will not be executed (generally, it only helps debugging) and return a field finger.

The QT library contains hundreds of internal detection records. When some errors are detected, warning messages are output.

Test the integrity and result correctness of QT based on the status of the following different debugging marks:

Check_state: Check the object state consistency. Check for consistent/expected object state
Check_range: Check that the range of the variable is incorrect.
Check_null: Check for dangerous null pointers.
Check_math: checks for dangerous mathematical operations, such as dividing by 0.
No_check: Close all check _... flag.
Debug: Allows debugging code.
No_debug: Disable the debug flag.
By default, both Debug and all check flag are enabled. To Disable debug, define no_debug. to disable the check flag, 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
}

 

4. General bugs
Here is a public bug worth mentioning: if you include a q_object macro in class declaration and MoC running, and forget to link to MoC to generate the object code to your executable program, then you will get some very fuzzy error information.
Any link error message is about vtbl, _ vtbl, _ vtbl, or a lack of prompt information similar to this type of problem.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.