-
Notifications
You must be signed in to change notification settings - Fork 119
fix: free xcb_reply's with scoped smart pointers #4592
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: free xcb_reply's with scoped smart pointers #4592
Conversation
7da0273 to
f8fded7
Compare
AlanGriffiths
left a comment
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.
One readability tweak.
But this there are other xcb results in the code that could have the same treatment:
xcb_render_query_pict_formats_replyxcb_poll_for_eventxcb_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) |
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.
Can be simpler:
| 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))) |
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.
Thanks for the hint, addressed and applied for other use case of xcb_* functions you listed.
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.
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_cptrfor 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); | ||
|
|
Copilot
AI
Jan 5, 2026
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.
Empty line should be removed as it adds unnecessary whitespace after the closing brace.
AlanGriffiths
left a comment
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.
Just some include tweaking:
- the first header should be the interface offered by the .cpp file
- then local headers (typically those in the current directory)
- then mir headers
...
| #include "mir/c_memory.h" | ||
| #include "xcb_connection.h" | ||
| #include "xwayland_cursors.h" | ||
| #include "xwayland_log.h" |
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.
| #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> |
| #include "mir/c_memory.h" | ||
|
|
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.
| #include "mir/c_memory.h" | |
| #include <mir/c_memory.h> |
37cd2e2 to
741d20b
Compare
Thanks @AlanGriffiths , looks like my LSP is putting those out of order. Addressed comments and rebased with main now. |
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.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| auto refresh_rate = static_cast<double>(screen_reply->rate); | ||
| mir::log_debug("Detected %.2fHz host output refresh rate.", refresh_rate); | ||
| return screen_reply->rate; |
Copilot
AI
Jan 6, 2026
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.
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.
| 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; |
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.
Missing null check before dereferencing screen_reply
If there is a problem, then it is pre-existing: doesn't block this PR
AlanGriffiths
left a comment
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.
One possible simplification...
| auto refresh_rate = static_cast<double>(screen_reply->rate); | ||
| mir::log_debug("Detected %.2fHz host output refresh rate.", refresh_rate); | ||
| return screen_reply->rate; |
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.
Missing null check before dereferencing screen_reply
If there is a problem, then it is pre-existing: doesn't block this PR
Co-authored-by: Alan Griffiths <alan@octopull.co.uk>
3acfdf9 to
c0957e5
Compare
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