Fix multiple mem leaks, refactor and improve memory management #10

Manually merged
jabber.developer merged 4 commits from ref/scrolling into master 2025-07-02 18:48:51 +00:00
  • I ran valgrind when using my new feature

How to test the functionality

  • Use Python plugins that use chat- and other hooks
  • Use page up/down in a regular chat multiple times so history is fetched
  • Use page up above DB history in MAM-enabled chat
<!--- Make sure to read CONTRIBUTING.md --> <!--- It mentions the rules to follow and helpful tools --> <!-- For completed items, change [ ] to [x]. --> - [x] I ran valgrind when using my new feature ### How to test the functionality - Use Python plugins that use chat- and other hooks - Use page up/down in a regular chat multiple times so history is fetched - Use page up above DB history in MAM-enabled chat
jabber.developer added 2 commits 2025-07-02 13:06:51 +00:00
- Introduce static helper `_truncate_datetime_suffix()` to safely trim datetime strings, removing unwanted suffixes like timezone offsets
- Replace manual string management with auto_gchar and g_strdup for safer, clearer ownership and to prevent leaks
- Add safety checks and logging warnings for unexpected datetime string lengths or null pointers
- Refactor _mam_rsm_id_handler to use the helper function and updated string handling
- Change log_database_get_previous_chat parameters for consistent ownership semantics, avoiding double frees and mem leaks
- Overall improve stability and prevent memory leaks during log database queries
Fix memory leak in _handle_string_or_none_result
All checks were successful
CI / Check spelling (pull_request) Successful in 4m23s
CI / Check coding style (pull_request) Successful in 10m37s
CI / Linux (fedora) (pull_request) Successful in 11m27s
CI / Linux (debian) (pull_request) Successful in 12m48s
CI / Linux (arch) (pull_request) Successful in 16m54s
CI / Linux (ubuntu) (pull_request) Successful in 48m55s
6871a4d46f
The function did not decref the Python object on type error paths,
resulting in a reference count leak. This caused memory to accumulate
over time. Added Py_XDECREF on all early return paths to properly free
the Python object reference and prevent leaks.
jabber.developer reviewed 2025-07-02 13:26:25 +00:00
src/ui/chatwin.c Outdated
@@ -589,3 +589,2 @@
chatwin_db_history(ProfChatWin* chatwin, const gchar* start_time, const gchar* end_time, gboolean flip)
{
if (!end_time) {
end_time = buffer_size(((ProfWin*)chatwin)->layout->buffer) == 0 ? NULL : g_date_time_format_iso8601(buffer_get_entry(((ProfWin*)chatwin)->layout->buffer, 0)->time);
Author
Owner

Mem leak fixed. Since end_time became const, it's not freed within the method.

auto_gchar gchar* _end_time is used as a temporary variable with autocleanup in case if end_time is not defined.

Mem leak fixed. Since `end_time` became _const_, it's not freed within the method. auto_gchar gchar* _end_time is used as a temporary variable with autocleanup in case if `end_time` is not defined.
jabber.developer marked this conversation as resolved
src/ui/window.c Outdated
@@ -716,3 +717,1 @@
GDateTime* now = g_date_time_new_now_local();
gchar* end_date = g_date_time_format_iso8601(now);
g_date_time_unref(now);
auto_gchar gchar* end_date = g_date_time_format_iso8601(now);
Author
Owner

Always free end_date now, since chatwin_db_history stopped managing memory.

Always free `end_date` now, since `chatwin_db_history` stopped managing memory.
jabber.developer marked this conversation as resolved
@@ -2754,1 +2754,4 @@
// Trim the last 3 characters from the datetime string to remove unwanted suffix (timezone offset)
static void
_truncate_datetime_suffix(gchar* str)
Author
Owner

Safe datetime truncation in case of malformed datetime

Safe datetime truncation in case of malformed datetime
jabber.developer marked this conversation as resolved
@@ -645,3 +645,3 @@
int max_sessions = accounts_get_max_sessions(account_name);
if (max_sessions > 0) {
const gchar* cur_resource = accounts_get_resource(account_name);
auto_gchar gchar* cur_resource = accounts_get_resource(account_name);
Author
Owner

Old mem leak fixed.

Old mem leak fixed.
jabber.developer marked this conversation as resolved
jabber.developer added 2 commits 2025-07-02 17:27:20 +00:00
Use newer version of Python's .supp file.
fix(window): preserve scroll offset on page_down and refactor
All checks were successful
CI / Check spelling (pull_request) Successful in 29s
CI / Check coding style (pull_request) Successful in 2m22s
CI / Linux (debian) (pull_request) Successful in 11m28s
CI / Linux (fedora) (pull_request) Successful in 16m46s
CI / Linux (arch) (pull_request) Successful in 20m29s
CI / Linux (ubuntu) (pull_request) Successful in 35m7s
CI / Check spelling (push) Successful in 17s
CI / Check coding style (push) Successful in 31s
CI / Linux (ubuntu) (push) Successful in 11m11s
CI / Linux (debian) (push) Successful in 13m26s
CI / Linux (fedora) (push) Successful in 16m10s
CI / Linux (arch) (push) Successful in 32m49s
f636090fd9
When paging down beyond the visible chat buffer, the scroll position could
jump unexpectedly. This occurred because the visual distance from the bottom
(e.g. 6 lines until the last message) wasn’t preserved after loading new
messages from the DB.

This patch fixes that by capturing the visual offset (`current_offset`) before
the DB fetch and reapplying it after, based on the y-position of the final
buffer entry. This ensures consistent and smooth scroll transitions, even
as message heights vary.

In addition to fixing the visual glitch, this commit introduces a targeted
performance optimization: once the bottom of the chat has been reached
(`WIN_SCROLL_REACHED_BOTTOM`), we skip expensive operations entirely —
including datetime lookups and entry formatting — during further page_down
events. This reduces performance stalls
during rapid paging (e.g. holding the Page Down key at the bottom of the scrolling area).

This update also adapts to the recent change in `chatwin_db_history()`,
which now returns a `db_history_result_t` instead of `gboolean`, allowing
correct differentiation between an empty result and an actual DB error.

Together, these changes:
- Fix a scroll-jump bug in `page_down`
- Improve performance and responsiveness during rapid navigation
- Align with improved DB result handling
jabber.developer manually merged commit f636090fd9 into master 2025-07-02 18:48:51 +00:00
Sign in to join this conversation.
No description provided.