Skip to content

Commit

Permalink
Freed some memory to prevent memory leaks or memory increase
Browse files Browse the repository at this point in the history
  • Loading branch information
nicola02nb committed Sep 19, 2024
1 parent 19e075e commit 84eca3f
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 14 deletions.
7 changes: 4 additions & 3 deletions src/DataTypes/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,15 @@ void Device::updateDevice(const Device *new_device)
this->chatmix = new_device->chatmix;
}

void Device::updateDevice(const QList<Device *> &new_device_list)
bool Device::updateDevice(const QList<Device *> &new_device_list)
{
for (int i = 0; i < new_device_list.length(); ++i) {
if (this != new_device_list.at(i)) {
this->updateDevice(new_device_list.at(i));
break;
return true;
}
}
return false;
}

QJsonObject Device::toJson() const
Expand Down Expand Up @@ -205,7 +206,7 @@ QList<Device *> mergeDevices(QList<Device *> connectedDevices, const QList<Devic

if (!deviceFound) {
// If the device wasn't found in saved devices, add it
connectedDevices.append(new Device(*savedDevice));
connectedDevices.append(savedDevice);
}
}
return connectedDevices;
Expand Down
2 changes: 1 addition & 1 deletion src/DataTypes/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class Device
bool operator==(const Device *d) const;

void updateDevice(const Device *new_device);
void updateDevice(const QList<Device *> &new_device_list);
bool updateDevice(const QList<Device *> &new_device_list);

QJsonObject toJson() const;
static Device fromJson(const QJsonObject &json);
Expand Down
44 changes: 35 additions & 9 deletions src/UI/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, trayIcon(new QSystemTrayIcon(this))
, trayMenu(new QMenu(this))
, timerGUI(new QTimer(this))
, API(HeadsetControlAPI(HEADSETCONTROL_FILE_PATH))
{
Expand Down Expand Up @@ -46,9 +47,19 @@ MainWindow::MainWindow(QWidget *parent)
hide();
}

void MainWindow::deleteDevices(QList<Device *> deviceList)
{
for (Device *device : deviceList) {
delete device;
}
deviceList.clear();
}

MainWindow::~MainWindow()
{
timerGUI->stop();
delete timerGUI;
delete trayMenu;
delete trayIcon;
delete ui;
}
Expand Down Expand Up @@ -169,7 +180,6 @@ void MainWindow::setupTrayIcon()
changeTrayIconTo("headphones");
trayIcon->setToolTip("HeadsetControl");

trayMenu = new QMenu(this);
trayMenu->addAction(tr("Hide/Show"), this, &MainWindow::toggleWindow);
ledOn = trayMenu->addAction(tr("Turn Lights On"), &API, [=]() {
API.setLights(selectedDevice, true);
Expand Down Expand Up @@ -300,10 +310,15 @@ void MainWindow::resetGUI()
//Devices Managing Section
void MainWindow::loadDevices()
{
QList<Device *> c = API.getConnectedDevices(), s = getSavedDevices();
n_connected = c.length();
n_saved = s.length();
deviceList = mergeDevices(c, s);
QList<Device *> connected = API.getConnectedDevices(), saved = getSavedDevices();
n_connected = connected.length();
n_saved = saved.length();
QList<Device *> merged = mergeDevices(connected, saved);

deleteDevices(deviceList);
deleteDevices(saved);

deviceList = merged;
}

void MainWindow::loadDevice(int deviceIndex)
Expand Down Expand Up @@ -484,7 +499,11 @@ QList<Device *> MainWindow::getSavedDevices()
void MainWindow::updateDevice()
{
QList<Device *> newDl = API.getConnectedDevices();
selectedDevice->updateDevice(newDl);
if (!selectedDevice->updateDevice(newDl)) {
selectedDevice = nullptr;
}

deleteDevices(newDl);
}

//Update GUI Section
Expand Down Expand Up @@ -686,6 +705,7 @@ void MainWindow::selectDevice()
loadDevice(index);
}
}
delete (loadDevWindow);
}

void MainWindow::editProgramSetting()
Expand All @@ -697,6 +717,7 @@ void MainWindow::editProgramSetting()
timerGUI->setInterval(settings.msecUpdateIntervalTime);
updateStyle();
}
delete (settingsW);
}

void MainWindow::checkForUpdates(bool firstStart)
Expand Down Expand Up @@ -733,7 +754,8 @@ void MainWindow::checkForUpdates(bool firstStart)
QString text = "HeadesetControl: " + s1 + "<br/>HeadesetControl-GUI: " + s2;
dialogWindow->setLabel(text);

dialogWindow->show();
dialogWindow->exec();
delete (dialogWindow);
}
}

Expand All @@ -750,7 +772,9 @@ void MainWindow::showAbout()
+ qApp->applicationVersion();
dialogWindow->setLabel(text);

dialogWindow->show();
dialogWindow->exec();

delete (dialogWindow);
}

void MainWindow::showCredits()
Expand All @@ -762,5 +786,7 @@ void MainWindow::showCredits()
"href='https://github.com/Sapd/HeadsetControl'>HeadsetCoontrol");
dialogWindow->setLabel(text);

dialogWindow->show();
dialogWindow->exec();

delete (dialogWindow);
}
4 changes: 3 additions & 1 deletion src/UI/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ class MainWindow : public QMainWindow

Ui::MainWindow *ui;
QSystemTrayIcon *trayIcon;
QTimer *timerGUI;
QString trayIconName;
QMenu *trayMenu;
QAction *ledOn;
QAction *ledOff;
QTimer *timerGUI;

Settings settings;

Expand All @@ -60,6 +60,8 @@ class MainWindow : public QMainWindow

QList<QSlider *> slidersEq;

void deleteDevices(QList<Device *> deviceList);

void bindEvents();

//Tray Icon Section
Expand Down
2 changes: 2 additions & 0 deletions src/Utils/headsetcontrolapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ QString HeadsetControlAPI::sendCommand(const QStringList &args_list)
// qDebug() << output;
qDebug() << "Error: \t" << proc->error();

delete (proc);

return output;
}

Expand Down

0 comments on commit 84eca3f

Please sign in to comment.