QT 5.15.2 QImage SegmentationFault

0

I'm dealing with an error I can not solve. I'm trying to show post processed camera images from c++ class to QT Component but I'm getting a Segmentation Fault and I cannot get any clue of how to fix it:

Here is my header file:

#ifndef _CAMERA_VIEW_H_
#define _CAMERA_VIEW_H_

#include <QtQuick>

class CameraView : public QQuickPaintedItem
{
    Q_OBJECT
    QML_ELEMENT
    Q_DISABLE_COPY(CameraView)

public:
    explicit CameraView(QQuickItem* parent = nullptr);
    void paint(QPainter *painter);

public slots:
    void updateImage(const QImage&);

protected:
    QImage m_image;
};

#endif

Here my cc class:

#include "core/CameraView.hpp"
#include "core/moc_CameraView.cpp"
#include <spdlog/spdlog.h>

CameraView::CameraView(QQuickItem* parent):QQuickPaintedItem(parent){}

void CameraView::updateImage(const QImage &img)
{
    qDebug() << Q_FUNC_INFO << "Image Valid, updating image...";
    m_image = img.copy(); // does shallow copy of image data
    update();             // triggers actual update
}


void CameraView::paint(QPainter *painter) {
    if(m_image.isNull())
        return;
    qDebug() << Q_FUNC_INFO << "paint requested...";

    QRectF bounding_rect = boundingRect();
    QImage scaled = m_image.scaledToHeight(bounding_rect.height());
    QPointF center = bounding_rect.center() - scaled.rect().center();
    if (center.x() < 0)
        center.setX(0);
    if (center.y() < 0)
        center.setY(0);
    painter->drawImage(center, scaled);
}

Here the part of my QML file using this component:

GridLayout {
    id: plateDetector
    objectName: "plateDetector"
    columns: 1
    rowSpacing: parent.height *.1
    Layout.preferredHeight: parent.height
    Layout.preferredWidth: parent.width


   CameraView {
       id: plateViewer
       objectName: "plateViewer"
       renderTarget: "FramebufferObject"
       Layout.preferredWidth: parent.width
       Layout.preferredHeight: parent.height *0.7
   }
}

Code for slot invocation:

cvtColor(drawMat, drawMat, cv::COLOR_BGR2RGB);
QImage image= QImage((uchar*) drawMat.data, drawMat.cols, drawMat.rows, drawMat.step, QImage::Format_RGB888);
emit updateImage(image);

Code for connections:

    //Getting plate manager controller elements
    QObject *plateManagerObject = findElement<QObject *>(objects, "plateManager");

    if(!plateView){
        QCoreApplication::exit(-1);
    }

    //Connect detector plate controller signals
    PlateDetectorController plateController(db, nullptr);
    spdlog::info("Plate Viewer loaded");
    QObject::connect(&plateController, &PlateDetectorController::updateImage, plateView, &CameraView::updateImage);

I tried to debug it with gdb with no success also tried some alternatives to copy the images and render the images but that's doesn't work.

Here gdb output:

pi@rasp:~/SmartGate/build $ gdb
GNU gdb (GDB) 10.1
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "armv7l-unknown-linux-gnueabihf".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word".
(gdb) file SmartGate
Reading symbols from SmartGate...
(gdb) r
Starting program: /home/pi/SmartGate/build/SmartGate 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/arm-linux-gnueabihf/libthread_db.so.1".
QML debugging is enabled. Only use this in a safe environment.
[New Thread 0xafc721e0 (LWP 17660)]
[2021-01-21 13:36:02.487] [info] Looking for scripts in folder resource/db/
[2021-01-21 13:36:02.488] [info] Running script in file resource/db/plate.sql
[2021-01-21 13:36:02.489] [info] Data base structure initialization done
[New Thread 0xae9b31e0 (LWP 17661)]
[New Thread 0xa7dc31e0 (LWP 17662)]
[2021-01-21 13:36:17.978] [info] Plate Viewer loaded
[New Thread 0xa71ff1e0 (LWP 17663)]
[New Thread 0xa55d31e0 (LWP 17664)]
[New Thread 0xa451d1e0 (LWP 17665)]
[New Thread 0xa3bff1e0 (LWP 17666)]
[New Thread 0xa31ff1e0 (LWP 17667)]
void CameraView::updateImage(const QImage&) Validating image...
void CameraView::updateImage(const QImage&) Image Valid, updating image...

Thread 1 "SmartGate" received signal SIGSEGV, Segmentation fault.
0xb6fb9bf0 in memcpy () from /usr/lib/arm-linux-gnueabihf/libarmmem-v7l.so
(gdb) bt
#0  0xb6fb9bf0 in memcpy () from /usr/lib/arm-linux-gnueabihf/libarmmem-v7l.so
#1  0x00000000 in ?? ()

It is failing in raspberry pi 4. I try this code in other linux distributions and works correctly.

c++
qt
qml
qt-quick
qimage
asked on Stack Overflow Jan 21, 2021 by Miguel Resendiz

1 Answer

1

Dupe of QT C++ How to return QImage from QThread and display it on QLabel in QMainWindow? - Please read what the QImage ctor tells you: The buffer must remain valid throughout the life of the QImage

Your data goes out-of-scope before (I assume you signal is in a different thread)

answered on Stack Overflow Jan 22, 2021 by chehrlic

User contributions licensed under CC BY-SA 3.0