QT 5.15.2 QImage分割错误

QT 5.15.2 QImage SegmentationFault

我正在处理一个我无法解决的错误。我正在尝试将 post 已处理的相机图像从 c++ class 显示到 QT 组件,但我遇到了分段错误,我无法获得任何修复方法的线索:

这是我的头文件:

#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

这是我的抄送 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);
}

这里是我的 QML 文件中使用这个组件的部分:

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
   }
}

插槽调用代码:

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

连接代码:

    //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);

我尝试用 gdb 调试它但没有成功,也尝试了一些替代方法来复制图像和渲染图像,但那是行不通的。

此处 gdb 输出:

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 ?? ()

它在 raspberry pi 4 中失败。我在其他 linux 发行版中尝试此代码并正常工作。

Dupe of - Please read what the QImage ctor 告诉你:缓冲区必须在 QImage 的整个生命周期内保持有效

你的数据之前超出了范围(我假设你的信号在不同的线程中)