Skip to content

Conversation

@aliihsancengiz
Copy link
Contributor

Closes #4559

Related: #4559

What's new?

Previously, some of xcb_*_reply results were never freed; this PR uses a scope-based smart pointer to free results when they get out of scope.

How to test

Checklist

  • Tests added and pass
  • Adequate documentation added
  • (optional) Added Screenshots or videos

@aliihsancengiz aliihsancengiz requested a review from a team as a code owner December 26, 2025 20:05
@aliihsancengiz aliihsancengiz force-pushed the fix/free_xcb_replies branch 2 times, most recently from 7da0273 to f8fded7 Compare December 28, 2025 18:42
Copy link
Contributor

@AlanGriffiths AlanGriffiths left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One readability tweak.

But this there are other xcb results in the code that could have the same treatment:

  • xcb_render_query_pict_formats_reply
  • xcb_poll_for_event
  • xcb_list_properties_reply

Either these should be addressed, or the issue should remain open

{
auto cookie = xcb_get_geometry(xcb_conn, win);
if (auto reply = xcb_get_geometry_reply(xcb_conn, cookie, nullptr))
if (auto const reply = mir::make_unique_cptr(xcb_get_geometry_reply(xcb_conn, cookie, nullptr)); reply)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be simpler:

Suggested change
if (auto const reply = mir::make_unique_cptr(xcb_get_geometry_reply(xcb_conn, cookie, nullptr)); reply)
if (auto const reply = mir::make_unique_cptr(xcb_get_geometry_reply(xcb_conn, cookie, nullptr)))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the hint, addressed and applied for other use case of xcb_* functions you listed.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses memory leaks in Xwayland-related code by replacing manual free() calls with scope-based smart pointers (mir::make_unique_cptr). The changes ensure that XCB reply objects are automatically freed when they go out of scope, preventing resource leaks that could occur if early returns or exceptions prevented the manual free() calls from being reached.

Key changes:

  • Wrapped XCB reply pointers in mir::make_unique_cptr for automatic memory management
  • Updated code to use .get() when accessing the underlying pointer
  • Added necessary #include <mir/c_memory.h> headers

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
src/server/frontend_xwayland/xwayland_wm.cpp Converted multiple XCB reply allocations to use smart pointers (xfixes_reply, event polling, and property listing)
src/server/frontend_xwayland/xwayland_cursors.cpp Wrapped formats_reply in smart pointer and added required header
src/platforms/x11/x11_resources.cpp Applied smart pointers to atom interning and refresh rate queries
src/platforms/x11/graphics/egl_helper.cpp Converted geometry reply to use smart pointer

}

free(formats_reply);

Copy link

Copilot AI Jan 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty line should be removed as it adds unnecessary whitespace after the closing brace.

Copilot uses AI. Check for mistakes.
Copy link
Contributor

@AlanGriffiths AlanGriffiths left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some include tweaking:

  1. the first header should be the interface offered by the .cpp file
  2. then local headers (typically those in the current directory)
  3. then mir headers
    ...

Comment on lines 21 to 24
#include "mir/c_memory.h"
#include "xcb_connection.h"
#include "xwayland_cursors.h"
#include "xwayland_log.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#include "mir/c_memory.h"
#include "xcb_connection.h"
#include "xwayland_cursors.h"
#include "xwayland_log.h"
#include "xwayland_cursors.h"
#include "xcb_connection.h"
#include "xwayland_log.h"
#include <mir/c_memory.h>

Comment on lines 18 to 19
#include "mir/c_memory.h"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#include "mir/c_memory.h"
#include <mir/c_memory.h>

@aliihsancengiz aliihsancengiz force-pushed the fix/free_xcb_replies branch 2 times, most recently from 37cd2e2 to 741d20b Compare January 5, 2026 17:56
@aliihsancengiz
Copy link
Contributor Author

aliihsancengiz commented Jan 5, 2026

Just some include tweaking:

  1. the first header should be the interface offered by the .cpp file
  2. then local headers (typically those in the current directory)
  3. then mir headers
    ...

Thanks @AlanGriffiths , looks like my LSP is putting those out of order.

Addressed comments and rebased with main now.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

Comment on lines 113 to 115
auto refresh_rate = static_cast<double>(screen_reply->rate);
mir::log_debug("Detected %.2fHz host output refresh rate.", refresh_rate);
return screen_reply->rate;
Copy link

Copilot AI Jan 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing null check before dereferencing screen_reply. If xcb_randr_get_screen_info_reply returns nullptr, line 113 will cause a null pointer dereference. Add a null check similar to the pattern used in other files.

Suggested change
auto refresh_rate = static_cast<double>(screen_reply->rate);
mir::log_debug("Detected %.2fHz host output refresh rate.", refresh_rate);
return screen_reply->rate;
if (!screen_reply)
{
mir::log_warning("Failed to get RANDR screen info; assuming 60Hz host output refresh rate.");
return 60.0;
}
auto refresh_rate = static_cast<double>(screen_reply->rate);
mir::log_debug("Detected %.2fHz host output refresh rate.", refresh_rate);
return refresh_rate;

Copilot uses AI. Check for mistakes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing null check before dereferencing screen_reply

If there is a problem, then it is pre-existing: doesn't block this PR

Copy link
Contributor

@AlanGriffiths AlanGriffiths left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One possible simplification...

Comment on lines 113 to 115
auto refresh_rate = static_cast<double>(screen_reply->rate);
mir::log_debug("Detected %.2fHz host output refresh rate.", refresh_rate);
return screen_reply->rate;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing null check before dereferencing screen_reply

If there is a problem, then it is pre-existing: doesn't block this PR

@AlanGriffiths AlanGriffiths added this pull request to the merge queue Jan 6, 2026
Merged via the queue into canonical:main with commit 49bb8a4 Jan 6, 2026
29 of 30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

XCB replies not always freed

2 participants