简体   繁体   English

QT 5.15.2 QImage SegmentationFault

[英]QT 5.15.2 QImage SegmentationFault

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:我正在尝试显示从 c++ class 到 QT 组件的后处理相机图像,但我得到一个分段错误,我无法获得任何关于如何修复它的线索:

Here is my header file:这是我的 header 文件:

#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:这是我的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:这是我使用此组件的 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
   }
}

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.我尝试使用 gdb 对其进行调试,但没有成功还尝试了一些替代方法来复制图像并渲染图像,但这不起作用。

Here gdb output:这里 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.它在树莓派 4 中失败。我在其他 linux 发行版中尝试此代码并正常工作。

Dupe of QT C++ How to return QImage from QThread and display it on QLabel in QMainWindow? 重复 QT C++ 如何从 QThread 返回 QImage 并将其显示在 QMainWindow 的 QLabel 上? - Please read what the QImage ctor tells you: The buffer must remain valid throughout the life of the QImage - 请阅读QImage ctor告诉您的内容:缓冲区必须在 QImage 的整个生命周期内保持有效

Your data goes out-of-scope before (I assume you signal is in a different thread)您的数据之前超出范围(我假设您的信号在不同的线程中)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM