- Qt Creator Documentation
- How to
- Analyzing code
- Qt Creator 19.0.0

View type hierarchy ◦ Analyze CPU usage

# Analyzing code

> How to: Analyze

Use integrated code analysis tools to detect memory leaks, profile function execution, analyze CPU use, and eliminate unnecessary complexity of code. Other integrated tools provide code coverage and visualize trace events.

To use a code analysis tool, select it in the Analyze menu or in the pull-down menu of the (Start Debugging of Startup Project) button. In the Debug mode, switch between tools by selecting them in the menu on the debugger toolbar.

Drag the views in the Debug mode to new positions on the screen. The size and position of views are saved for future sessions. Select View > Views > Reset to Default Layout to reset the views to their original sizes and positions.

## Improving QML performance

Qt Creator comes with the QML Profiler for inspecting binding evaluations and signal handling when running QML code. This is useful for identifying potential bottlenecks, especially in the evaluation of bindings.

## Finding issues in QML code

Run static checks on the QML and JavaScript code in your project to find common problems, similarly to using JSLint.

## Finding issues in C++ code with Clang tools

Qt Creator comes with the following Clang tools for finding problems in C, C++, and Objective-C source code by using static analysis:

- Clang-Tidy, which has diagnostics and fixes for typical programming errors, such as style violations or interface misuse.
- Clazy, which helps Clang understand Qt semantics. It displays Qt-related compiler warnings, ranging from unnecessary memory allocation to misuse of API and has refactoring actions for fixing some of the issues.

## Finding issues in C++ code with Cppcheck

Install the Cppcheck static analysis tool to detect undefined behavior and dangerous coding constructs in C++ code. Cppcheck analyzes the source code without actually running the application.

## Checking code coverage

Install the Coco code coverage toolchain for Tcl, QML, C# and C/C++ programs on macOS, Linux, or Windows to analyze the way an application runs as part of a test suite, for example. Use the results to make the tests more efficient and complete.

You can:

- Find untested code sections.
- Find redundant tests which can then be eliminated. Coco can identify portions of the source code that are covered by a test. It can detect whether a new test covers lines in the source code that the existing tests do not cover.
- Find dead code by displaying code that is never executed.
- Calculate the optimum test execution order so as to maximize test coverage for each run. This is particularly useful for manual testing.
- Analyze two separate versions of an application and compare the differences. This makes it possible to see which tests are affected by source code modifications and also to get some measure of the test coverage of a patch or hot fix.
- Measure the execution time of applications and tests.

## Preventing code erosion

Install Axivion Suite to protect software from erosion. With static code analysis, architecture analysis, and code-smells-detection, you can:

- Check the source code for potential runtime errors.
- Use metrics to generate quantitative information about the internal quality of the source code.
- Run style checks to achieve compliance with coding guidelines.
- Detect both duplicates and similar pieces of code in the source code.
- Recognize cyclical dependencies at different levels.
- Detect unreachable code.

## Profiling with Valgrind tools

Install Memcheck and Callgrind from Valgrind's Tool Suite to detect memory leaks and profile function execution.

You can run the Valgrind tools either locally on the development host or remotely on another host. You can use them to analyze both applications for which you set up a project in Qt Creator and applications for which you do not have a project.

Valgrind tools are supported locally only on Linux and macOS. However, according to Valgrind.org, support on macOS 10.8 and 10.9 is experimental and mostly broken. You can run the tools on a remote Linux machine or device from any development host.

To run the Valgrind tools to analyze an application for which you have a project, open the project in Qt Creator and select the kit to run the project. The kit specifies whether the Valgrind tools are run locally or remotely.

For more information about analyzing applications for which you do not have a project, see Run Valgrind tools on external applications.

To set preferences for the Valgrind tools, select Preferences > Analyzer > Valgrind. You can override the general settings for each project in the Run Settings for the project.

## Detecting memory leaks with Heob

On Windows, install the Heob heap observer to detect buffer overruns and memory leaks.

## Analyzing CPU usage

On Linux, use Perf to analyze the CPU and memory usage of an application on Linux desktop and embedded devices.

Performance Analyzer uses the Perf tool bundled with the Linux kernel to take periodic snapshots of the call chain of an application and visualizes them in a timeline view or as a flame graph.

## Visualizing Chrome trace events

Use the Chrome Trace Format Visualizer to view Chrome trace events. This is especially useful when viewing large trace files that are difficult to visualize using the built-in trace-viewer (`chrome://tracing`).

See also Activate kits for a project, How to: Analyze, and Analyzers.

View type hierarchy ◦ Analyze CPU usage