c ++ – Qt: The capture of camera images continuously fills all memory

I'm trying to capture images continuously so I can send them in UDP format. I do it to set up a live video streaming program.

The code below captures the images continuously and assigns images to QGraphicsScene so that I can check if the images are read like a video. But when I run the program, my computer crashes even if I delete the pointers. How can I optimize this code?

#include "mainwindow.h"
#include "ui_mainwindow.h"
#understand 

MainWindow :: MainWindow (QWidget * parent):
QMainWindow (parent),
ui (new Ui :: MainWindow)
{
ui-> setupUi (this);
scene = new QGraphicsScene (this);
ui-> graphicsView-> setScene (scene);
cam = new QCamera;
cam-> setCaptureMode (QCamera :: CaptureStillImage);

viewfinder = new QCameraViewfinder;
viewfinder-> show ();
QCameraImageCapture * cap = new QCameraImageCapture (cam);
cap-> setCaptureDestination (QCameraImageCapture :: CaptureToBuffer);

cam-> setViewfinder (viewfinder);


QObject :: connect (cap, & QCameraImageCapture :: imageCaptured, [=] (int id, QImage img) {

while (true) {
QByteArray * buf = new QByteArray;
QBuffer * buffer = new QBuffer (buf);
buffer-> open (QIODevice :: WriteOnly);
img.save (buffer, "BMP");
QPixmap * pixmap = new QPixmap ();
pixmap-> loadFromData (buffer-> buffer ());
scene-> addPixmap (* pixmap);
delete buf;
delete the buffer;
remove pixmap;

QThread :: sleep (0.0416);
cap-> capture ();

}

});

QObject :: connect (cap, & QCameraImageCapture :: readyForCaptureChanged, [=] (Boolean state) {
if (state == true) {
cam-> searchAndLock ();
cap-> capture ();
cam-> unlock ();
}
});
cam-> start ();
}

MainWindow :: ~ MainWindow ()
{
delete ui;
}