From 16ed7cc18720820e22fba881fc50df7ead42ffb2 Mon Sep 17 00:00:00 2001 From: John Hernandez <129467592+H3rnand3zzz@users.noreply.github.com> Date: Thu, 9 Nov 2023 09:33:51 +0100 Subject: [PATCH] Refactor `plugins_pre_chat_message_display` for Memory Management Refactor function to enhance memory handling, addressing temporary workaround introduced in 2e0adbd. Adjustments ensure cleaner code and maintainability. Part of the improvements suggested by @sjaeckel. --- src/plugins/plugins.c | 7 +++---- src/plugins/plugins.h | 2 +- src/ui/chatwin.c | 13 +++---------- 3 files changed, 7 insertions(+), 15 deletions(-) diff --git a/src/plugins/plugins.c b/src/plugins/plugins.c index 3dccc881..3b64c7aa 100644 --- a/src/plugins/plugins.c +++ b/src/plugins/plugins.c @@ -477,10 +477,10 @@ plugins_on_disconnect(const char* const account_name, const char* const fulljid) } char* -plugins_pre_chat_message_display(const char* const barejid, const char* const resource, const char* message) +plugins_pre_chat_message_display(const char* const barejid, const char* const resource, char* message) { char* new_message = NULL; - char* curr_message = strdup(message); + char* curr_message = message; GList* values = g_hash_table_get_values(plugins); GList* curr = values; @@ -489,8 +489,7 @@ plugins_pre_chat_message_display(const char* const barejid, const char* const re new_message = plugin->pre_chat_message_display(plugin, barejid, resource, curr_message); if (new_message) { free(curr_message); - curr_message = strdup(new_message); - free(new_message); + curr_message = new_message; } curr = g_list_next(curr); } diff --git a/src/plugins/plugins.h b/src/plugins/plugins.h index 48423b64..0bd69e41 100644 --- a/src/plugins/plugins.h +++ b/src/plugins/plugins.h @@ -134,7 +134,7 @@ void plugins_on_shutdown(void); void plugins_on_connect(const char* const account_name, const char* const fulljid); void plugins_on_disconnect(const char* const account_name, const char* const fulljid); -char* plugins_pre_chat_message_display(const char* const barejid, const char* const resource, const char* message); +char* plugins_pre_chat_message_display(const char* const barejid, const char* const resource, char* message); void plugins_post_chat_message_display(const char* const barejid, const char* const resource, const char* message); char* plugins_pre_chat_message_send(const char* const barejid, const char* message); void plugins_post_chat_message_send(const char* const barejid, const char* message); diff --git a/src/ui/chatwin.c b/src/ui/chatwin.c index c9690c39..bf302cd0 100644 --- a/src/ui/chatwin.c +++ b/src/ui/chatwin.c @@ -322,7 +322,9 @@ chatwin_incoming_msg(ProfChatWin* chatwin, ProfMessage* message, gboolean win_cr char* old_plain = message->plain; - message->plain = plugins_pre_chat_message_display(message->from_jid->barejid, message->from_jid->resourcepart, message->plain); + auto_char char* new_plain = plugins_pre_chat_message_display(message->from_jid->barejid, message->from_jid->resourcepart, strdup(message->plain)); + message->plain = new_plain; + gboolean show_message = true; ProfWin* window = (ProfWin*)chatwin; @@ -411,7 +413,6 @@ chatwin_incoming_msg(ProfChatWin* chatwin, ProfMessage* message, gboolean win_cr plugins_post_chat_message_display(message->from_jid->barejid, message->from_jid->resourcepart, message->plain); - free(message->plain); message->plain = old_plain; } @@ -575,11 +576,7 @@ _chatwin_history(ProfChatWin* chatwin, const char* const contact_barejid) while (curr) { ProfMessage* msg = curr->data; - char* msg_plain = msg->plain; msg->plain = plugins_pre_chat_message_display(msg->from_jid->barejid, msg->from_jid->resourcepart, msg->plain); - // This is dirty workaround for memory leak. We reassign msg->plain above so have to free previous object - // TODO: Make a better solution, for example, pass msg object to the function and it will replace msg->plain properly if needed. - free(msg_plain); win_print_history((ProfWin*)chatwin, msg); curr = g_slist_next(curr); } @@ -605,11 +602,7 @@ chatwin_db_history(ProfChatWin* chatwin, const char* start_time, char* end_time, while (curr) { ProfMessage* msg = curr->data; - char* msg_plain = msg->plain; msg->plain = plugins_pre_chat_message_display(msg->from_jid->barejid, msg->from_jid->resourcepart, msg->plain); - // This is dirty workaround for memory leak. We reassign msg->plain above so have to free previous object - // TODO: Make a better solution, for example, pass msg object to the function and it will replace msg->plain properly if needed. - free(msg_plain); if (flip) { win_print_old_history((ProfWin*)chatwin, msg); } else {