Skip to content

Commit

Permalink
Revise plugins_pre_chat_message_display for Memory Management
Browse files Browse the repository at this point in the history
Refactor function to enhance memory handling, addressing
temporary workaround introduced in 2e0adbd. Adjustments
ensure cleaner code and maintainability.
  • Loading branch information
H3rnand3zzz committed Nov 11, 2023
1 parent bba8278 commit e950ab9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
9 changes: 9 additions & 0 deletions src/plugins/plugins.c
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,15 @@ plugins_pre_chat_message_display(const char* const barejid, const char* const re
return curr_message;
}

char*
plugins_pre_chat_message_display_and_free(const char* const barejid, const char* const resource, char* message)
{
char* tmp = plugins_pre_chat_message_display(barejid, resource, message);
free(message);
message = NULL;
return tmp;
}

void
plugins_post_chat_message_display(const char* const barejid, const char* const resource, const char* message)
{
Expand Down
1 change: 1 addition & 0 deletions src/plugins/plugins.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ void plugins_on_connect(const char* const account_name, const char* const fullji
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_and_free(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);
Expand Down
12 changes: 2 additions & 10 deletions src/ui/chatwin.c
Original file line number Diff line number Diff line change
Expand Up @@ -575,11 +575,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);
msg->plain = plugins_pre_chat_message_display_and_free(msg->from_jid->barejid, msg->from_jid->resourcepart, msg->plain);
win_print_history((ProfWin*)chatwin, msg);
curr = g_slist_next(curr);
}
Expand All @@ -605,11 +601,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);
msg->plain = plugins_pre_chat_message_display_and_free(msg->from_jid->barejid, msg->from_jid->resourcepart, msg->plain);
if (flip) {
win_print_old_history((ProfWin*)chatwin, msg);
} else {
Expand Down

0 comments on commit e950ab9

Please sign in to comment.