Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QObject: Cannot create children for a parent that is in a different thread. When Deleting Object.



  • I keep getting this Waring in my console out put when I call delete.

    I build up a QList of objects that I allocated dynamically. When the destructor is called I delete the objs from the list then clear the list.

    I tried following the Qt documentation for deletion but still get the warning

    QList<Employee *> list;
    list.append(new Employee("Blackpool", "Stephen"));
    list.append(new Employee("Twist", "Oliver"));
    
    qDeleteAll(list.begin(), list.end());
    list.clear();
    

    I have tried using deleteLater() but doesn't resolve the issue. I tired moving the thread the objects are created in. Not quite sure how to resolve this warning. That about using smart pointers since they de-allocate automatically...

    Any Ideas?

    CPrint.cpp
    
    CPrint::~CPrint()
    {
        qDebug() << "########################## CPrint::~CPrint() destructor";
        deletePlotList();
    }
    
    /**
     * @brief delete the plots list and its references
     */
    void CPrint::deletePlotList()
    {
        qDebug() << "########################## CPrint::deletePlotList() m_plost size 1: " << m_plots.size();
        while (!m_plots.isEmpty()) {
            delete m_plots.takeFirst();
            qDebug() << "########################## CPrint::deletePlotList() m_plost size 2: " << m_plots.size();
        }
        m_plots.clear();
        qDebug() << "########################## CPrint::deletePlotList() m_plost size 3: " << m_plots.size();
    }
    
    /**
     * @brief Prepares Graph for printing
     *
     * @param vName
     * @param x
     * @param y
     *
     * @return Bool
     */
    bool CPrint::drawGraph(const QString &vName, const QVector<double> &x, const QVector<double> &y)
    {
        qDebug() << "########################## CPrint::drawGraph() ";
    
        bool mResult = true;
        QCustomPlot *mPlot = new QCustomPlot();
        m_plots.append(mPlot);
    
        mPlot->setFont(m_painter.font());
        mPlot->xAxis->setLabelFont(m_painter.font());
        mPlot->yAxis->setLabelFont(m_painter.font());
    
        mPlot->addGraph();
        mPlot->graph(0)->setData(x, y);
    
        mPlot->xAxis->setLabel(vName);//"Time");
        mPlot->xAxis->setTickLabels(false);
    
        mPlot->yAxis->setLabel(tr("Intensity"));
        mPlot->yAxis->setTickLabels(false);
    
        mPlot->xAxis->setRange(m_xAxisMin, m_xAxisMax);
        mPlot->yAxis->setRange(m_yAxisMin, m_yAxisMax);
    
        mPlot->toPainter(&m_painter , PLOT_WIDTH, PLOT_HEIGHT, PLOT_X, m_yStart + m_ygap);
        m_yStart += PLOT_HEIGHT;
    
        return mResult;
    }
    
    CPrint.h
    
    QList<QCustomPlot* > m_plots;
    

    2019-11-12 14:00:09.102, DEBUG, ########################## CPrint::drawGraph() ,on line #361
    2019-11-12 14:00:09.106, DEBUG, ########################## CPrint::drawLine() ,on line #341
    2019-11-12 14:00:09.106, DEBUG, ########################## CPrint::drawLine() ,on line #341
    2019-11-12 14:00:09.107, DEBUG, ########################## CPrint::drawEnd() ,on line #396
    2019-11-12 14:00:09.168, DEBUG, ########################## CPrint::~CPrint() destructor,on line #152
    2019-11-12 14:00:09.169, DEBUG, ########################## CPrint::deletePlotList() m_plost size 1: 26,on line #161
    2019-11-12 14:00:09.170, WARNING, QObject: Cannot create children for a parent that is in a different thread.(Parent is QApplication(0x7ffdf4425d90), parent's thread is QThread(0x9f57ae0), current thread is QThread(0x82fc840),on line #0
    2019-11-12 14:00:09.171, DEBUG, ########################## CPrint::deletePlotList() m_plost size 2: 25,on line #164
    2019-11-12 14:00:09.171, DEBUG, ########################## CPrint::deletePlotList() m_plost size 2: 24,on line #164
    2019-11-12 14:00:09.172, DEBUG, ########################## CPrint::deletePlotList() m_plost size 2: 23,on line #164
    2019-11-12 14:00:09.172, DEBUG, ########################## CPrint::deletePlotList() m_plost size 2: 22,on line #164
    2019-11-12 14:00:09.173, DEBUG, ########################## CPrint::deletePlotList() m_plost size 2: 21,on line #164
    2019-11-12 14:00:09.173, DEBUG, ########################## CPrint::deletePlotList() m_plost size 2: 20,on line #164
    2019-11-12 14:00:09.173, DEBUG, ########################## CPrint::deletePlotList() m_plost size 2: 19,on line #164
    2019-11-12 14:00:09.174, DEBUG, ########################## CPrint::deletePlotList() m_plost size 2: 18,on line #164
    2019-11-12 14:00:09.174, DEBUG, ########################## CPrint::deletePlotList() m_plost size 2: 17,on line #164
    2019-11-12 14:00:09.174, DEBUG, ########################## CPrint::deletePlotList() m_plost size 2: 16,on line #164
    2019-11-12 14:00:09.175, DEBUG, ########################## CPrint::deletePlotList() m_plost size 2: 15,on line #164
    2019-11-12 14:00:09.180, DEBUG, ########################## CPrint::deletePlotList() m_plost size 2: 14,on line #164
    2019-11-12 14:00:09.181, DEBUG, ########################## CPrint::deletePlotList() m_plost size 2: 13,on line #164
    2019-11-12 14:00:09.181, DEBUG, ########################## CPrint::deletePlotList() m_plost size 2: 12,on line #164
    2019-11-12 14:00:09.182, DEBUG, ########################## CPrint::deletePlotList() m_plost size 2: 11,on line #164
    2019-11-12 14:00:09.183, DEBUG, ########################## CPrint::deletePlotList() m_plost size 2: 10,on line #164
    2019-11-12 14:00:09.187, DEBUG, ########################## CPrint::deletePlotList() m_plost size 2: 9,on line #164
    2019-11-12 14:00:09.187, DEBUG, ########################## CPrint::deletePlotList() m_plost size 2: 8,on line #164
    2019-11-12 14:00:09.188, DEBUG, ########################## CPrint::deletePlotList() m_plost size 2: 7,on line #164
    2019-11-12 14:00:09.188, DEBUG, ########################## CPrint::deletePlotList() m_plost size 2: 6,on line #164
    2019-11-12 14:00:09.189, DEBUG, ########################## CPrint::deletePlotList() m_plost size 2: 5,on line #164
    2019-11-12 14:00:09.189, DEBUG, ########################## CPrint::deletePlotList() m_plost size 2: 4,on line #164
    2019-11-12 14:00:09.221, DEBUG, ########################## CPrint::deletePlotList() m_plost size 2: 3,on line #164
    2019-11-12 14:00:09.221, DEBUG, ########################## CPrint::deletePlotList() m_plost size 2: 2,on line #164
    2019-11-12 14:00:09.222, DEBUG, ########################## CPrint::deletePlotList() m_plost size 2: 1,on line #164
    2019-11-12 14:00:09.223, DEBUG, ########################## CPrint::deletePlotList() m_plost size 2: 0,on line #164
    2019-11-12 14:00:09.224, DEBUG, ########################## CPrint::deletePlotList() m_plost size 3: 0,on line #167


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Where is your threading code ?
    What do you do in it ?
    Are you creating widgets in that thread ? If that's the case then it's wrong. Widgets creation and manipulation should be done in the QApplication thread i.e the main thread


Log in to reply