Skip to content

Commit cbfc915

Browse files
authored
Merge pull request #117 from sysprog21/fix-memory-bugs
Fix memory leaks in widget/window lifecycle
2 parents 3828b96 + 8715567 commit cbfc915

File tree

5 files changed

+26
-0
lines changed

5 files changed

+26
-0
lines changed

src/box.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,17 @@ twin_dispatch_result_t _twin_box_dispatch(twin_widget_t *widget,
158158
_twin_widget_dispatch(widget, event) == TwinDispatchDone)
159159
return TwinDispatchDone;
160160
switch (event->kind) {
161+
case TwinEventDestroy:
162+
/* Destroy all children first */
163+
while (box->children) {
164+
child = box->children;
165+
box->children = child->next;
166+
167+
/* Send destroy event to child */
168+
ev.kind = TwinEventDestroy;
169+
(*child->dispatch)(child, &ev);
170+
}
171+
break;
161172
case TwinEventQueryGeometry:
162173
return _twin_box_query_geometry(box);
163174
case TwinEventConfigure:

src/label.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ twin_dispatch_result_t _twin_label_dispatch(twin_widget_t *widget,
7373
case TwinEventQueryGeometry:
7474
_twin_label_query_geometry(label);
7575
break;
76+
case TwinEventDestroy:
77+
/* Free the label text */
78+
if (label->label)
79+
free(label->label);
80+
break;
7681
default:
7782
break;
7883
}

src/pixmap.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ twin_pixmap_t *twin_pixmap_create(twin_format_t format,
5151
#if defined(CONFIG_DROP_SHADOW)
5252
pixmap->shadow = false;
5353
#endif
54+
pixmap->window = NULL; /* Initialize window field */
5455
pixmap->p.v = pixmap + 1;
5556
memset(pixmap->p.v, '\0', space);
5657
return pixmap;
@@ -80,6 +81,7 @@ twin_pixmap_t *twin_pixmap_create_const(twin_format_t format,
8081
pixmap->origin_x = pixmap->origin_y = 0;
8182
pixmap->stride = stride;
8283
pixmap->disable = 0;
84+
pixmap->window = NULL; /* Initialize window field */
8385
pixmap->p = pixels;
8486
return pixmap;
8587
}

src/widget.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ twin_dispatch_result_t _twin_widget_dispatch(twin_widget_t *widget,
9696
_twin_widget_paint(widget);
9797
widget->paint = false;
9898
break;
99+
case TwinEventDestroy:
100+
/* Base widget has no special cleanup */
101+
break;
99102
default:
100103
break;
101104
}

src/window.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ twin_window_t *twin_window_create(twin_screen_t *screen,
9595
void twin_window_destroy(twin_window_t *window)
9696
{
9797
twin_window_hide(window);
98+
99+
/* Call the destroy callback if set to clean up window contents */
100+
if (window->destroy)
101+
(*window->destroy)(window);
102+
98103
twin_pixmap_destroy(window->pixmap);
99104
free(window->name);
100105
free(window);

0 commit comments

Comments
 (0)