-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Fix multiple memory leaks in Region::process() function #4284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix multiple memory leaks in Region::process() function #4284
Conversation
- 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.
- Remove trailing whitespace - Normalize line endings - Ensure compliance with project's clang-format rules Fixes CI test-clang-format workflow failure.
|
✅ Fixed clang-format compliance issue Applied
This should resolve the CI |
Introduced a cleanup lambda to handle memory deallocation in Region::process, replacing repeated code with a single reusable function for better maintainability and readability.
Reformatted the cleanup lambda in Region::process for improved readability by expanding it to multiple lines.
|
@mmahmoudian bro can you review this once? |
|
This is best to be reviewed by @@borgmanJeremy |
|
@borgmanJeremy Please review this |
|
Thanks for finding this memory leak. Could you please change the pointers to unique_ptrs? Then the cleanup function can be removed. As is it's likely future refactoring won't always call cleanup. Thanks! |
Replaces manual memory management with std::unique_ptr for temporary QApplication and argument variables. Removes redundant cleanup logic for improved safety and readability.
|
@borgmanJeremy great catch, I've done the required changes review that |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you !
|
Thanks for the contribution and patience :) |
Description
Fixes multiple memory leaks in the
Region::process()function whereQApplication,char**array, andint*pointer objects were not properly cleaned up before returning.Changes Made
QApplication* tempAppbefore all return statementschar** argvarray usingdelete[]int* argcpointer usingdeleteCode Changes
File:
src/utils/valuehandler.cppFunction:
Region::process()QVariant Region::process(const QVariant& val) { // FIXME: This is temporary, just before D-Bus is removed char** argv = new char*[1]; int* argc = new int{ 0 }; + QApplication* tempApp = nullptr; if (QGuiApplication::screens().empty()) { - new QApplication(*argc, argv); + tempApp = new QApplication(*argc, argv); } QString str = val.toString(); if (str == "all") { - return ScreenGrabber().desktopGeometry(); + QRect result = ScreenGrabber().desktopGeometry(); + // Cleanup allocated memory before returning + delete tempApp; + delete[] argv; + delete argc; + return result; } else if (str.startsWith("screen")) { bool ok; int number = str.mid(6).toInt(&ok); if (!ok || number < 0) { + // Cleanup allocated memory before returning + delete tempApp; + delete[] argv; + delete argc; return {}; } - return ScreenGrabber().screenGeometry(qApp->screens()[number]); + QRect result = ScreenGrabber().screenGeometry(qApp->screens()[number]); + // Cleanup allocated memory before returning + delete tempApp; + delete[] argv; + delete argc; + return result; } // ... regex processing ... if (!regex.match(str).hasMatch()) { + // Cleanup allocated memory before returning + delete tempApp; + delete[] argv; + delete argc; return {}; } // ... final processing ... if (!(w_ok && h_ok && x_ok && y_ok)) { + // Cleanup allocated memory before returning + delete tempApp; + delete[] argv; + delete argc; return {}; } - return QRect(x, y, w, h).normalized(); + QRect result = QRect(x, y, w, h).normalized(); + + // Cleanup allocated memory before returning + delete tempApp; + delete[] argv; + delete argc; + + return result; }Testing
Impact
Related Issue
Closes #4283
Type of Change
Checklist