c ++ – Qt: capturing images from the camera continuously fills the entire memory

I am trying to capture images continuously to be able to send them using UDP. I'm doing this to implement a live video broadcast program.

The following code continuously captures images and assigns images to QGraphicsScene so you can test whether the images are reproduced as a video. But when I run the program, my computer freezes even if I erase the pointers. How can I optimize this code?

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

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

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

cam-> setViewfinder (viewer);


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 buffer
delete pixmap;

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

}

});

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

Main window :: ~ Main window ()
{
eliminate ui;
}