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

QVideoFrame Crash on map - unmap



  • I have this function in my code and I am trying to show a frame of a video feed on a QLabel. I have subclassed QAbstractVideoSurface and pass the QVideoFrame using a signal. When I use the debugger it seems everything here gets done without any problems. But it crashes right after this.

    Any ideas?

    void MainWindow::onFrameAvailable(const QVideoFrame &frame)
    {
    QVideoFrame cloneFrame(frame);
    cloneFrame.map(QAbstractVideoBuffer::ReadOnly);

    const QImage image(cloneFrame.bits(),
                       cloneFrame.width(),
                       cloneFrame.height(),
                       QVideoFrame::imageFormatFromPixelFormat(cloneFrame.pixelFormat()));
    
    cloneFrame.unmap();
    ui->videoLabel->setPixmap(QPixmap::fromImage(image));
    

    }


  • Lifetime Qt Champion

    Hi,

    Your QImage relies on the data from cloneFrame. Since you unmap cloneFrame before using your image, it now tries to access data that are not valid anymore. See the constructor documentation about the lifetime of the data passed as parameter.



  • @SGaist said:

    Hi,

    Your QImage relies on the data from cloneFrame. Since you unmap cloneFrame before using your image, it now tries to access data that are not valid anymore. See the constructor documentation about the lifetime of the data passed as parameter.

    Uh my mistake. Thanks @SGaist . I used QImage::copy() method to copy it into a persistent memory space and now it's fine!


  • Lifetime Qt Champion

    If you only make the pixmap from the image, then moving the call to unmap after you're done is enough. That avoids a useless copy.



  • @SGaist said:

    If you only make the pixmap from the image, then moving the call to unmap after you're done is enough. That avoids a useless copy.

    Actually that was what I tried at first but it crashed again so I assumed QPixmap is also using the same space in the memory?! (Not so sure about this)



  • @SGaist but you're right the performance will drop because of the useless copy!


  • Lifetime Qt Champion

    You can also copy the pixmap itself, it might be faster.



  • @SGaist I'll give it a try! Thanks for the help anyway.


Log in to reply