Creation of QtChart object creates EXC_BAD_ACCESS error upon runtime

259 Views Asked by At

I currently trying to move some code from Python to C++ for practice reasons and tried to create a line chart with the QtCharts library. I installed the Qt library with the Qt open source installer provided by Qt on their website and finally managed to include the library in the project on Clion with the following code:

CMakelist.txt

cmake_minimum_required(VERSION 3.12)
project(Uebung1)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Qt
# Set this to your Qt installation
set(CMAKE_PREFIX_PATH /Users/DaniBook/Qt/5.11.2/clang_64)
set(RESOURCE_FOLDER res)
set(RESOURCE_FILES ${RESOURCE_FOLDER}/resources.qrc)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)

find_package(Qt5Charts CONFIG REQUIRED)

set(PROJECT_SOURCE_DIR src/)
include_directories(${PROJECT_SOURCE_DIR})
add_executable(Uebung1 src/main.cpp src/tools.h src/tools.cpp)
target_link_libraries(Uebung1 Qt5::Charts)

So far so good I was able to then use the library in my code which basically consists of a two functions. One that reads data from a file and converts it to a vector of integers and one that takes this vector and attempts to create a Qt line chart.

tools.cpp

#include "tools.h"
#include <vector>
#include <string>
#include <map>
#include <iostream>
#include <QtCharts>
#include <fstream>

using namespace std;

void GC_skew(vector<long> &gc_data, string &genome_file) {
    ifstream gfile(genome_file);
    string line;
    long g_sum = 0, c_sum = 0;

    if(gfile.is_open()) {
        //reading file line by line
        while(getline(gfile, line)) {
            //iterating over string characters to count G and C
            for(auto &character : line) {
                switch(character) {
                    case 'G' :
                        g_sum++;
                        break;
                    case 'C' :
                        c_sum++;
                        break;
                    default :
                        continue;
                }
                //appending skew to vector
                gc_data.push_back(g_sum - c_sum);
            }
        }
    }
    else {
        cout << "Unable to open file!" << endl;
    }
}

void skew_chart(vector<long> &gc_data) {
    auto *series = new QLineSeries();//creating new series object
    auto *chart = new QChart();              //creating new chart object
    auto *chartview = new QChartView(chart); //creating chartview object
    QMainWindow window;                     //window object for display
    long pos = 0;

    //iterating over the vector and appending data to series
    for(auto it : gc_data) {
        series->append(pos, it);
        pos++;
    }

    //adding series to chart and viewing chart
    chart->addSeries(series);
    chartview->setRenderHint(QPainter::Antialiasing);
    window.setCentralWidget(chartview);
    window.show();
}

main.cpp

#include <iostream>
#include "tools.h"
#include <vector>
#include <map>

using namespace std;

int main() {
    vector<long> gc_data = {};
    string genome_file = "<path_to_file>";

    GC_skew(gc_data, genome_file);
    skew_chart(gc_data);
}

The code compiles without an error but when running it terminates with exit code 11 (whatever that means). However, upon debugging I figured there is a problem with the creation of a new QChart object where I keep getting the following exception as stated by the debugger:

Exception = error: use of undeclared identifier 'Exception' //occurs somewhere before the one below but does not effect the application
Exception = EXC_BAD_ACCESS (code=1, address=0x0) //causes the application to crash

Debug information

The way of initialization above is exactly the way given by Qt (https://doc.qt.io/qt-5/qtcharts-linechart-example.html) additional research in their resources did not lead to any solution. Does anybody of you might know what to do?

P.S.: by stepping through the lines one by one I discovered a jump upon initialization into a header which seems to define an exception which is raised at whatever event

qtflags.h (part of Qt lib)

Q_DECL_CONSTEXPR inline QFlags(Zero = Q_NULLPTR) Q_DECL_NOTHROW : i(0) {}

however the stack trace of the debugger indicates a successful object creation.

stack trace of debugger

The Qt version is 5.11.2 Compiler version Apple LLVM version 10.0.0 (clang-1000.11.45.2)

Thanks in advance if anybody can make sense of this

1

There are 1 best solutions below

1
On BEST ANSWER

You dont have any QApplication instance.

Here's a quote from Qt Docs:

The QApplication class manages the GUI application's control flow and main settings.

QApplication contains the main event loop, where all events from the window system and other sources are processed and dispatched. It also handles the application's initialization, finalization, and provides session management. In addition, QApplication handles most of the system-wide and application-wide settings.

For any GUI application using Qt, there is precisely one QApplication object, no matter whether the application has 0, 1, 2 or more windows at any given time. For non-GUI Qt applications, use QCoreApplication instead, as it does not depend on the QtGui library.

The QApplication object is accessible through the instance() function that returns a pointer equivalent to the global qApp pointer.

So, the line

QApplication app(argc, argv);

creates an instance of the QApplication class.

In the end you need to call app.exec() which enters the main event loop and waits until exit() is called, then returns the value that was set to exit() (which is 0 if exit() is called via quit()).

It is necessary to call this function to start event handling. The main event loop receives events from the window system and dispatches these to the application widgets.