Skip to content

Multiple memory leaks in Region::process() function #4283

@avineet4

Description

@avineet4

Description

Multiple memory leaks exist in the Region::process() function in src/utils/valuehandler.cpp. The function allocates memory for QApplication, char** array, and int* pointer but never cleans them up before returning.

Location

  • File: src/utils/valuehandler.cpp
  • Function: Region::process() (lines 530-590)
  • Lines affected: 533-536, and all return statements

Current Code (Problematic)

// Memory allocated but never cleaned up
char** argv = new char*[1];           // Line 533 - never deleted
int* argc = new int{ 0 };            // Line 534 - never deleted
if (QGuiApplication::screens().empty()) {
    new QApplication(*argc, argv);   // Line 536 - never deleted
}
// ... function continues with multiple return statements
// None of the allocated memory is cleaned up before returning

Expected Behavior

All allocated memory should be properly cleaned up before the function returns, regardless of which return path is taken.

Impact

  • Multiple memory leaks on every call to Region::process()
  • QApplication objects accumulate in memory
  • char and int arrays** leak with each call
  • Potential application instability under heavy usage
  • Violates RAII principles

Proposed Fix

QVariant Region::process(const QVariant& val)
{
    char** argv = new char*[1];
    int* argc = new int{ 0 };
    QApplication* tempApp = nullptr;
    if (QGuiApplication::screens().empty()) {
        tempApp = new QApplication(*argc, argv);
    }
    
    // ... process logic ...
    
    // Cleanup before every return statement
    delete tempApp;
    delete[] argv;
    delete argc;
    return result;
}

Steps to Reproduce

  1. Call Region::process() with any valid input
  2. Monitor memory usage - memory will leak with each call
  3. Repeat multiple times to see accumulation

Environment

  • Any platform where Flameshot runs
  • Occurs on every call to Region::process()

Priority

High - Multiple memory leaks that occur on every function call, can cause significant memory accumulation over time.

Additional Notes

  • The function has a FIXME comment indicating this is temporary code before D-Bus removal
  • Even temporary code should not leak memory
  • This affects the configuration parsing system

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions