Skip to content

Commit 3602d0a

Browse files
authored
Fix multiple memory leaks in Region::process() function (#4284)
* Fix multiple memory leaks in Region::process() function - Add proper cleanup for allocated QApplication object - Add proper cleanup for allocated char** argv array - Add proper cleanup for allocated int* argc pointer - Ensure all return paths properly clean up allocated memory - Prevents memory leaks when Region::process() is called Fixes multiple memory leaks identified in codebase analysis. * Apply clang-format to fix code style compliance - Remove trailing whitespace - Normalize line endings - Ensure compliance with project's clang-format rules Fixes CI test-clang-format workflow failure. * Refactor cleanup logic in Region::process Introduced a cleanup lambda to handle memory deallocation in Region::process, replacing repeated code with a single reusable function for better maintainability and readability. * Format cleanup lambda for readability Reformatted the cleanup lambda in Region::process for improved readability by expanding it to multiple lines. * Refactor Region::process to use smart pointers Replaces manual memory management with std::unique_ptr for temporary QApplication and argument variables. Removes redundant cleanup logic for improved safety and readability.
1 parent 5b0ee2f commit 3602d0a

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/utils/valuehandler.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <QRegularExpression>
1111
#include <QStandardPaths>
1212
#include <QVariant>
13+
#include <memory>
1314

1415
// VALUE HANDLER
1516

@@ -530,10 +531,11 @@ bool Region::check(const QVariant& val)
530531
QVariant Region::process(const QVariant& val)
531532
{
532533
// FIXME: This is temporary, just before D-Bus is removed
533-
char** argv = new char*[1];
534-
int* argc = new int{ 0 };
534+
auto argv = std::make_unique<char*[]>(1);
535+
auto argc = std::make_unique<int>(0);
536+
std::unique_ptr<QApplication> tempApp;
535537
if (QGuiApplication::screens().empty()) {
536-
new QApplication(*argc, argv);
538+
tempApp = std::make_unique<QApplication>(*argc, argv.get());
537539
}
538540

539541
QString str = val.toString();

0 commit comments

Comments
 (0)