Skip to content

Fix memory leaks in widget/window lifecycle #117

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

Merged
merged 1 commit into from
Jul 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/box.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,17 @@ twin_dispatch_result_t _twin_box_dispatch(twin_widget_t *widget,
_twin_widget_dispatch(widget, event) == TwinDispatchDone)
return TwinDispatchDone;
switch (event->kind) {
case TwinEventDestroy:
/* Destroy all children first */
while (box->children) {
child = box->children;
box->children = child->next;

/* Send destroy event to child */
ev.kind = TwinEventDestroy;
(*child->dispatch)(child, &ev);
}
break;
case TwinEventQueryGeometry:
return _twin_box_query_geometry(box);
case TwinEventConfigure:
Expand Down
5 changes: 5 additions & 0 deletions src/label.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ twin_dispatch_result_t _twin_label_dispatch(twin_widget_t *widget,
case TwinEventQueryGeometry:
_twin_label_query_geometry(label);
break;
case TwinEventDestroy:
/* Free the label text */
if (label->label)
free(label->label);
break;
default:
break;
}
Expand Down
2 changes: 2 additions & 0 deletions src/pixmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ twin_pixmap_t *twin_pixmap_create(twin_format_t format,
#if defined(CONFIG_DROP_SHADOW)
pixmap->shadow = false;
#endif
pixmap->window = NULL; /* Initialize window field */
pixmap->p.v = pixmap + 1;
memset(pixmap->p.v, '\0', space);
return pixmap;
Expand Down Expand Up @@ -80,6 +81,7 @@ twin_pixmap_t *twin_pixmap_create_const(twin_format_t format,
pixmap->origin_x = pixmap->origin_y = 0;
pixmap->stride = stride;
pixmap->disable = 0;
pixmap->window = NULL; /* Initialize window field */
pixmap->p = pixels;
return pixmap;
}
Expand Down
3 changes: 3 additions & 0 deletions src/widget.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ twin_dispatch_result_t _twin_widget_dispatch(twin_widget_t *widget,
_twin_widget_paint(widget);
widget->paint = false;
break;
case TwinEventDestroy:
/* Base widget has no special cleanup */
break;
default:
break;
}
Expand Down
5 changes: 5 additions & 0 deletions src/window.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ twin_window_t *twin_window_create(twin_screen_t *screen,
void twin_window_destroy(twin_window_t *window)
{
twin_window_hide(window);

/* Call the destroy callback if set to clean up window contents */
if (window->destroy)
(*window->destroy)(window);

twin_pixmap_destroy(window->pixmap);
free(window->name);
free(window);
Expand Down