Application won't always start
-
I'm learning Qt for C++ (with Python I made pretty good progress) and I have notices that sometimes my window won't pop-up, logs in the window constructor won't get printed (
qDebug()<<"message..."
and similar type of messages).I'm communicating with my DLL to access serial port, so I though this may have an impact, but after removing it, I don't see difference. Still, out of 10 attempts to start
.exe
, Window will show maybe around 60% of the times.Some other info:
- Windows 11
- Qt version 6.5
- CMake as build system, developed under vscode
- Qt paths set accordingly
- There is no error at all, application seems to start and do something in the background, but windows doesn't open
If it's needed, below is
main.c
andmainwindow.cpp
.#include "mainwindow.h" #include <QApplication> int main(int argc, char* argv[]) { QApplication a(argc, argv); //These 2 prints sometimes don't show in the console at all qDebug() << "Before MainWindow constructor"; MainWindow w; qDebug() << "After MainWindow constructor"; w.show(); return a.exec(); }
mainwindow class constructor
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); /* Create custom views */ view_can_config = new ViewCanConfig(); view_debug = new ViewDebug(); view_fw_update = new ViewFwUpdate(); view_logger = new ViewLogger(); view_tx_msg_config = new ViewTxMsgConfig(); views_list.append(view_can_config); views_list.append(view_debug); views_list.append(view_fw_update); views_list.append(view_logger); views_list.append(view_tx_msg_config); connect(view_tx_msg_config, ViewTxMsgConfig::onSendMessage, this, MainWindow::send_can_message); connect(view_logger, ViewLogger::onSendMessage, this, MainWindow::send_can_message); /* Connect debug messages */ connect(view_can_config, ViewCanConfig::send_debug, view_debug, ViewDebug::add_debug_message); connect(view_debug, ViewDebug::send_debug, view_debug, ViewDebug::add_debug_message); connect(view_fw_update, ViewFwUpdate::send_debug, view_debug, ViewDebug::add_debug_message); connect(view_logger, ViewLogger::send_debug, view_debug, ViewDebug::add_debug_message); connect(view_tx_msg_config, ViewTxMsgConfig::send_debug, view_debug, ViewDebug::add_debug_message); connect(this, MainWindow::send_debug, view_debug, ViewDebug::add_debug_message); /* Add views to the layouts */ ui->layout_tab_logger->addWidget(view_logger); ui->layout_tab_debug->addWidget(view_debug); ui->layout_tab_fdcan_config->addWidget(view_can_config); ui->layout_tab_tx_msg_config->addWidget(view_tx_msg_config); ui->layout_tab_fw_update->addWidget(view_fw_update); /* Add quick TX widget to the logger view using default configuration */ view_logger->add_quick_tx_widget(view_tx_msg_config->create_new_tx_msg_config_window()); /* Set status bar */ statusbar = new StatusBar(this); ui->statusbar->addWidget(statusbar); /* Triggers */ connect(ui->btn_com_port_refresh, &QPushButton::clicked, this, &MainWindow::load_com_ports); connect(ui->btn_com_port_connect_disconnect, &QPushButton::clicked, this, &MainWindow::toggle_connect_to_port); /* Get system com ports */ #if 1 load_com_ports(); /* Reset values */ ca_evt_read_ptr = 0; ca_evt_write_ptr = 0; /* Queue processing timer */ timer_queue = new QTimer(this); timer_queue->setSingleShot(false); timer_queue->setInterval(50); connect(timer_queue, &QTimer::timeout, this, &MainWindow::timer_ca_queue_check_expired); timer_queue->start(); emit send_debug(CaUtils::DebugLevel::Info, "APP", "Application running"); /* Initialize CANanalyzer interface */ if (ca_init((ca_evt_fn)&prv_ca_evt_fn)) { emit send_debug(CaUtils::DebugLevel::Info, "DLL", "App ready to use"); } #endif }
Am I missing something? What could be common reasons that app wouldn't always start properly?
-
Found the problem. It was uninitialized variable in another class that was constructed in the constructor of
MainWindow
, causing for loop to go to to 2^32 or other random number.Sorry for taking your time for this.
The Before MainWindow... was NOT always printed. It may have to do with flushing of the data in the system.
I will continue monitoring but it looks good for the moment.
-
@tilz0R
How is the application compiled and how is it launched?
Are you always launching the debug build?To narrow the issue down: Use
qInfo()
instead ofqDebug()
and start your executable from the command line. Watch out for error messages on startup. -
Compiled with CMake:
# configure cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_TOOLCHAIN_FILE=D:/.../cmakefile/x86_64-w64-mingw32-gcc.cmake -SD:/.../gui_cpp -BD:/.../build/Win64-Debug -G Ninja # Build cmake --build path/to/dir
Toolchain file
set(CMAKE_SYSTEM_NAME Windows) # Some default GCC settings set(CMAKE_C_COMPILER x86_64-w64-mingw32-gcc) set(CMAKE_CXX_COMPILER x86_64-w64-mingw32-g++) set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
Toolchain
x86_64-w64-mingw32-gcc --version x86_64-w64-mingw32-gcc (Rev6, Built by MSYS2 project) 12.2.0 Copyright (C) 2022 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
CMakeLists file
cmake_minimum_required(VERSION 3.22) project("CANanalyzer" VERSION 0.1 LANGUAGES C CXX) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTOUIC ON) set(CMAKE_AUTORCC ON) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) #set(CMAKE_PREFIX_PATH "c:/Qt6.5.0/6.5.0/mingw_64/") find_package(QT NAMES Qt6 REQUIRED COMPONENTS Widgets) find_package(QT NAMES Qt6 REQUIRED COMPONENTS Core) find_package(QT NAMES Qt6 REQUIRED COMPONENTS SerialPort) find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets) find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core) find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS SerialPort) # Define include paths set(PROJECT_INCLUDES ${CMAKE_CURRENT_LIST_DIR}/ ${CMAKE_CURRENT_LIST_DIR}/include ${CMAKE_CURRENT_LIST_DIR}/models ${CMAKE_CURRENT_LIST_DIR}/widgetss ${CMAKE_CURRENT_LIST_DIR}/views ${CMAKE_CURRENT_LIST_DIR}/../common/include ${CMAKE_CURRENT_LIST_DIR}/../common/proto ${CMAKE_CURRENT_LIST_DIR}/../libs/nanopb ${CMAKE_CURRENT_LIST_DIR}/../libs/mbedtls/include ) # Define project sources set(PROJECT_SOURCES ${CMAKE_CURRENT_LIST_DIR}/main.cpp ${CMAKE_CURRENT_LIST_DIR}/mainwindow.cpp # Widgets & views ${CMAKE_CURRENT_LIST_DIR}/widgets/tx_msg_config_entry_widget.cpp ${CMAKE_CURRENT_LIST_DIR}/widgets/can_clock_config_entry_widget.cpp ${CMAKE_CURRENT_LIST_DIR}/widgets/can_clock_config_entry_widget_timepar.cpp ${CMAKE_CURRENT_LIST_DIR}/widgets/statusbar.cpp ${CMAKE_CURRENT_LIST_DIR}/views/view_can_config.cpp ${CMAKE_CURRENT_LIST_DIR}/views/view_debug.cpp ${CMAKE_CURRENT_LIST_DIR}/views/view_logger.cpp ${CMAKE_CURRENT_LIST_DIR}/views/view_fw_update.cpp ${CMAKE_CURRENT_LIST_DIR}/views/view_tx_msg_config.cpp # Models ${CMAKE_CURRENT_LIST_DIR}/models/canloggerdatamodel.cpp # UI files ${CMAKE_CURRENT_LIST_DIR}/mainwindow.ui ${CMAKE_CURRENT_LIST_DIR}/widgets/can_clock_config_entry_widget.ui ${CMAKE_CURRENT_LIST_DIR}/widgets/tx_msg_config_entry_widget.ui ${CMAKE_CURRENT_LIST_DIR}/views/view_can_config.ui ${CMAKE_CURRENT_LIST_DIR}/views/view_logger.ui ${CMAKE_CURRENT_LIST_DIR}/views/view_fw_update.ui ${CMAKE_CURRENT_LIST_DIR}/views/view_debug.ui ${CMAKE_CURRENT_LIST_DIR}/views/view_tx_msg_config.ui # Missing this, MOC won't run for custom header files !! ${CMAKE_CURRENT_LIST_DIR}/views/custom_tab_widget_base.h ) # Add mbedTLS file(GLOB MBEDTLS_FILES ${CMAKE_CURRENT_LIST_DIR}/../libs/mbedtls/library/*.c) set(PROJECT_SOURCES ${PROJECT_SOURCES} ${MBEDTLS_FILES}) # Create executable file qt_add_executable(${CMAKE_PROJECT_NAME} MANUAL_FINALIZATION ${PROJECT_SOURCES} ) # Set application firmware version -> read from the file directly include(../cmake/version.cmake) # Add to build target_compile_definitions(${CMAKE_PROJECT_NAME} PUBLIC # Version setup "CA_FW_VERSION_MAJOR=${CA_FW_VERSION_MAJOR}" "CA_FW_VERSION_MINOR=${CA_FW_VERSION_MINOR}" "CA_FW_VERSION_PATCH=${CA_FW_VERSION_PATCH}" "CA_FW_VERSION_TYPE=${CA_FW_VERSION_TYPE}" "CA_FW_VERSION_REV=${CA_FW_VERSION_REV}" # Nano protobuf "PB_FIELD_32BIT" ) # Set include paths target_include_directories(${CMAKE_PROJECT_NAME} PUBLIC ${PROJECT_INCLUDES}) # Link Qt libraries target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::SerialPort ) # Add cananalyzer library target_link_directories(${PROJECT_NAME} PUBLIC build/Win64-Debug) target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE libCANanalyzer) set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} MACOSX_BUNDLE TRUE WIN32_EXECUTABLE OFF ) install(TARGETS ${CMAKE_PROJECT_NAME} BUNDLE DESTINATION . LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) if(QT_VERSION_MAJOR EQUAL 6) qt_finalize_executable(${CMAKE_PROJECT_NAME}) endif()
Start command
gui_cpp\build\Win64-Debug>.\CANanalyzer.exe Before MainWindow constructor
For instance, I got Before MainWindow constructor but then no After MainWindow message. No log messages are returned.
-
Then start it in your debugger and see why it crashes. Minimize the ctor until it works reliable.
-
@tilz0R
Ok, but the before constructor message is always printed, right? -
@tilz0R said in Application won't always start:
load_com_ports();
As @Christian-Ehrlicher said, you could start by commenting this line out.
I have no clue what the application actually does, but com ports can be a bit flaky at times. So maybe this function blocks.
-
Found the problem. It was uninitialized variable in another class that was constructed in the constructor of
MainWindow
, causing for loop to go to to 2^32 or other random number.Sorry for taking your time for this.
The Before MainWindow... was NOT always printed. It may have to do with flushing of the data in the system.
I will continue monitoring but it looks good for the moment.
-