Qt crashes when adding series to plot



  • Hello,

    I have a chart where I want to put in a QLineSeries, but every time it wants to add them the app crashes. My code looks like this:
    this is the creation of the plot, the I send it to another function where i will fill it.

        QChart *Dopplerplot = new QChart();
        QLineSeries *DSeries = new QLineSeries;
        Dopplerplot->addSeries(DSeries);
        QValueAxis *axisX = new QValueAxis;
        axisX->setRange(0, 2000);
        axisX->setLabelFormat("%g");
        axisX->setTitleText("Sample");
        QValueAxis *axisY = new QValueAxis;
        axisY->setRange(-1, 1);
        axisY->setTitleText("Speed");
        Dopplerplot->setAxisX(axisX, DSeries);
        Dopplerplot->setAxisY(axisY, DSeries);
        Dopplerplot->legend()->hide();
        Dopplerplot->setTitle("Doppler (m/s)");
    

    here I fill the lineseries with data I received:

        QScatterSeries *series = new QScatterSeries();
        doppler = 0;
        Dopplerplot->removeAllSeries();
        Dplot_y += 5;
        for(uint i = 0; i < ob.size(); ++i)
        {
            series->append(QPointF(ob.at(i).x, ob.at(i).y));
            doppler += ob.at(i).doppler;
        }
        if(doppler == 0)
        {
            doppler = 0.1;
        }
        else
        {
            doppler = doppler/ob.size();
        }
        DSeries->append(QPointF(Dplot_y, doppler));
        XZplot->removeAllSeries();
        XZplot->addSeries(series);
    

    And I have a timer running every second to draw the plot, this is to try when I slowed it down it would draw it but didn't help.

    void Radar::updateData()
    {
        Dopplerplot->addSeries(DSeries);
    }
    

    when I remove the addSeries line the app doesn't crash, I haven't found out where it crashes so I hope someone here could help me out.


  • Lifetime Qt Champion

    Hi,

    You should run your application through the debugger and get the stack trace to see where it fails exactly.

    By the way, which version of Qt are you using ? On which OS ? With which compiler ?



  • Hello,

    I am using Qt version 5.10 on a windows 10 pc with the mingw compiler.
    I did some debugging and it gives a segmentation fault with a qvector in this code it ends in the qvector.h file.

    template <typename T>
    void QVector<T>::append(const T &t)
    {
        const bool isTooSmall = uint(d->size + 1) > d->alloc;
        if (!isDetached() || isTooSmall) {
            T copy(t);
            QArrayData::AllocationOptions opt(isTooSmall ? QArrayData::Grow : QArrayData::Default);
            reallocData(d->size, isTooSmall ? d->size + 1 : d->alloc, opt);
    
            if (QTypeInfo<T>::isComplex)
                new (d->end()) T(qMove(copy));
            else
                *d->end() = qMove(copy);
    
        } else {
            if (QTypeInfo<T>::isComplex)
                new (d->end()) T(t);
            else
                *d->end() = t;
        }
        ++d->size;
    }
    

    does this mean that my vector is to small or that the values inside my vector are to small? the data in the point in the vector where it crashes it says 30.0, 0.1


  • Qt Champions 2018

    Hi @Vera,

    more interesting would be, how this append is called from your code. Are you using the vector with a pointer for example? Is the pointer valid?



  • hello @aha_1980,

    I found what was causing the crash it was the Dopplerplot->removeAllSeries(). That kinda makes sense but also not, the first time it does this there are no series on the plot but that shouldn't make it crash as it doesn't for my other plot.
    Now that it doesn't crash anymore it still doesn't plot any points to the plot. So maybe it is a bad pointer. I send the dopplerplot which I created in my main to my program like this:

    Radar *radar = new Radar(XZplot, Dopplerplot);
    

    And then in the radar program I do it like this:

    Radar::Radar(QChart *XZ, QChart *Dplot)
        :XZplot(XZ), Dopplerplot(Dplot)
    {
        dataport = new QSerialPort();
        cfgport = new QSerialPort();
        DSeries = new QLineSeries();
        MagicWord.append("0201040306050807");
        Dplot_y = 0;
        timer = new QTimer(this);
        connect(timer, SIGNAL(timeout()), this, SLOT(updateData()));
        connect(dataport, &QSerialPort::readyRead, this, &Radar::readData);
        Set_serial();
    }
    

    and XZplot and Dopplerplot are made in the hpp file like this:

        QChart *XZplot;
        QChart *Dopplerplot;
    

    Then I use them both in the draw_objects() function, do you see anything wrong in my code? Or do you need some more info?


  • Moderators

    @Vera said in Qt crashes when adding series to plot:

    Radar *radar = new Radar(XZplot, Dopplerplot);

    is Dopplerplot a valid pointer at that time?



  • @jsulm
    How can I check if it is a valid pointer?
    In my first post you can see how I created Dopplerplot, don't know if that helps.


  • Moderators

    @Vera said in Qt crashes when adding series to plot:

    How can I check if it is a valid pointer?

    You should know that. Do you create this

    QChart *Dopplerplot = new QChart();
    

    before radar?
    Also it looks to me like you are creating a local variable here:

    QChart *Dopplerplot = new QChart(); // This Dopplerplot is not the same as in header file!
    

    Remove QChart * there



  • @jsulm
    Well I am still learning C++ and pointers are always difficult.

    I do create the Dopplerplot before radar.
    I am creating a local variable as the one in the header file is for the radar class which is a private variable. and the creation of the Dopplerplot is in the main file where I create my GUI. But for my other plot which is also a QChart plot I did the same thing and that one works.



  • Hello,

    I got it working finally!
    I had to send the series to my radar class instead of the charts.
    So I had it like this before, where I send the charts over:

    Radar *radar = new Radar(XZplot, Dopplerplot);
    

    and now I did it like this, where I send the series over:

    Radar *radar = new Radar(series, DSeries);
    

    And now it plots perfectly, I just have to append new points to the series and it plots.

    Thanks all for your help!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.