Skip to content

Commit

Permalink
Merge pull request #111 from errose28/fix-remove
Browse files Browse the repository at this point in the history
Fix memory issues when removing bar items
  • Loading branch information
FelixKratz authored Nov 14, 2021
2 parents a86733a + 07b6b5c commit 2378cbd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/bar_item.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ void bar_item_inherit_from_item(struct bar_item* bar_item, struct bar_item* ance
text_destroy(&bar_item->label);

char* name = bar_item->name;
char* script = bar_item->name;
char* click_script = bar_item->name;
char* script = bar_item->script;
char* click_script = bar_item->click_script;

memcpy(bar_item, ancestor, sizeof(struct bar_item));

Expand Down
22 changes: 14 additions & 8 deletions src/bar_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,21 @@ void bar_manager_move_item(struct bar_manager* bar_manager, struct bar_item* ite

void bar_manager_remove_item(struct bar_manager* bar_manager, struct bar_item* bar_item) {
if (bar_manager->bar_item_count <= 0 || !bar_item) return;
struct bar_item* tmp[bar_manager->bar_item_count - 1];
int count = 0;
for (int i = 0; i < bar_manager->bar_item_count; i++) {
if (bar_manager->bar_items[i] == bar_item) continue;
tmp[count++] = bar_manager->bar_items[i];
if (bar_manager->bar_item_count == 1) {
free(bar_manager->bar_items);
bar_manager->bar_items = NULL;
bar_manager->bar_item_count = 0;
} else {
struct bar_item* tmp[bar_manager->bar_item_count - 1];
int count = 0;
for (int i = 0; i < bar_manager->bar_item_count; i++) {
if (bar_manager->bar_items[i] == bar_item) continue;
tmp[count++] = bar_manager->bar_items[i];
}
bar_manager->bar_item_count--;
bar_manager->bar_items = realloc(bar_manager->bar_items, sizeof(struct bar_item*)*bar_manager->bar_item_count);
memcpy(bar_manager->bar_items, tmp, sizeof(struct bar_item*)*bar_manager->bar_item_count);
}
bar_manager->bar_item_count--;
bar_manager->bar_items = realloc(bar_manager->bar_items, sizeof(struct bar_item*)*bar_manager->bar_item_count);
memcpy(bar_manager->bar_items, tmp, sizeof(struct bar_item*)*bar_manager->bar_item_count);
bar_item_destroy(bar_item);
}

Expand Down

0 comments on commit 2378cbd

Please sign in to comment.