gpt4 book ai didi

c++ - 在 Qt/QML 中的每一帧更新图像时运行时崩溃

转载 作者:太空宇宙 更新时间:2023-11-04 13:17:20 26 4
gpt4 key购买 nike

应用程序运行良好但随机崩溃,调试器显示此错误:

0xc0000005: read access violation,

QOpenGLFunctions_3_2_Compatibility::glBeginTransformFeedBack

但这并没有多大帮助,因为我不知道这个函数在哪里使用。

通过我的测试,我认为问题出在图像显示上。该程序从连接到相机的远程设备检索原始数据,应用程序正在显示它以在 QML 应用程序中显示视频。例如,当我在 QML 中评论图像的来源时,它不会崩溃。

在线程中:

bool success;
DWORD bytesRead;
DWORD lastError;
const unsigned int imgSize = IMG_HEIGHT * IMG_WIDTH * 1;

_rawData = new uchar[imgSize];

while(!_abort)
{
if(_namedPipe != INVALID_HANDLE_VALUE)
{
/* Get raw data from the pipe */
success = ReadFile(
_namedPipe,
_rawData,
imgSize,
&bytesRead,
NULL);

if(success)
{
/* Build an image from the raw data and send it to the PipeVideoStreamPlayer */
QImage clImage(_rawData, IMG_WIDTH, IMG_HEIGHT, QImage::Format_Indexed8);

_videostr->updateImage(clImage);
}else

...

图像提供者:

PipeVideoStreamPlayer::PipeVideoStreamPlayer(QQuickView* ptrApp)
: QQuickView(),
QQuickImageProvider(QQmlImageProviderBase::Image)
{
_ptrApp = ptrApp;
}

更新函数:

void
PipeVideoStreamPlayer::updateImage(const QImage& image)
{
_myImage = image;
QObject* objMain = (QObject*) _ptrApp->rootObject();
QQuickItem *obj = objMain->findChild<QQuickItem*>("videoStreamFrame");

QMetaObject::invokeMethod(obj, "reload");
}

在 QML 中,每帧调用图像 + 重新加载函数:

Image {
objectName: "videoStreamFrame"
cache: false
anchors.centerIn: parent
source: "image://videostream/yellow"

function reload() {
var oldSource = source;
source = "";
source = oldSource;
}

应用程序可以在崩溃前运行 5 分钟,有时只需 10 秒。如果我评论重新加载函数,它也不会崩溃。

编辑:

这是我到目前为止所做的修改:

class StreamPainter : public QObject
{
Q_OBJECT
Q_PROPERTY(QImage streamImage READ streamImage WRITE setStreamImage NOTIFY streamImageChanged)
public:
StreamPainter(QObject *parent = 0);
~StreamPainter();

QImage streamImage();
void setStreamImage(QImage clImage);

signals:
void streamImageChanged(QImage Image);

public slots:
private:
QImage _streamImage;
};

QImage StreamPainter::streamImage()
{
return _streamImage;
}

void StreamPainter::setStreamImage(QImage clImage)
{
_streamImage = clImage;
emit streamImageChanged(clImage);
}

设置上下文:

view->engine()->rootContext()->setContextProperty("streamImage", streamImage);

在我的主题中:

QImage clImage(_rawData, IMG_WIDTH, IMG_HEIGHT, QImage::Format_Indexed8);

_spvideostr->setStreamImage(clImage);

现在我需要在 QML 中显示图像,但是在没有 ImageProvider 的情况下如何在此处显示图像?

最佳答案

我并没有设法真正改变显示图像的代码,我认为很多问题都来自于我的程序中如何使用 thread 但目前我设法修复了通过在 QML 调用的 request 函数和 updateImage 调用的私有(private) _myImage 变量周围使用 mutex 来解决问题通过线程:

QImage PipeVideoStreamPlayer::requestImage(const QString & id, QSize * size, const QSize & requestedSize)
{
Q_UNUSED(id)
Q_UNUSED(size)
Q_UNUSED(requestedSize)

_mutex.lock();
_prevImage = _myImage;
_mutex.unlock();
return _prevImage;
}

_myImage 被锁定时,我发送上一张图像。由于这种情况发生的次数不多,我应该时不时丢失一些帧,但不会显示出来。

void
PipeVideoStreamPlayer::updateImage(const QImage& image)
{
_mutex.lock();
_myImage = image;
_mutex.unlock();
QObject* objMain = (QObject*) _ptrApp->rootObject();
QQuickItem *obj = objMain->findChild<QQuickItem*>("videoStreamFrame");

QMetaObject::invokeMethod(obj, "reload");
}

关于c++ - 在 Qt/QML 中的每一帧更新图像时运行时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36723045/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com