Skip to content

Commit e2d4ac0

Browse files
authored
Update README.md
1 parent 7073018 commit e2d4ac0

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,8 +531,22 @@ Note: Image display is a feature introduced experimentally in a fork of libui-ng
531531

532532
## Control Destruction and Memory Management
533533

534-
The ownership tree mechanism works well for most simple controls, allowing automatic memory deallocation through garbage collection.
535-
However, due to the unpredictable timing of GC execution, it is advisable to explicitly destroy the main window before `UIng.uninit` performs its memory leak check when in doubt.
534+
libui-ng is a C library, so normally the user is expected to manage memory manually.
535+
However, it also provides some automatic memory management. The basic rule is: when a parent control is destroyed, all of its child controls are automatically destroyed as well.
536+
537+
For example, when destroy is called on controls such as Window, Box, Grid, Group, or Tab, it first destroys all child controls and then destroys itself.
538+
539+
In the case of a Window, there are two common situations:
540+
541+
When the user clicks the close (×) button on the title bar
542+
In this case, on_closing is triggered, and Window#destroy is called automatically.
543+
544+
When on_should_quit is triggered
545+
This means the entire program is about to quit. But here, Window#destroy is not called automatically, so the user needs to call it explicitly.
546+
547+
Crystal has garbage collection (GC) and provides a finalize hook when objects are freed. But since the timing of GC is unpredictable, combining it with libui-ng’s own automatic destruction can easily lead to issues. That’s why this project does not use finalize hooks.
548+
549+
It may seem a bit tricky, but understanding how libui-ng handles memory will help you avoid problems when building applications.
536550

537551
Some controls require specific destruction order for proper memory cleanup
538552
- [#6](https://github.com/kojix2/uing/issues/6)

0 commit comments

Comments
 (0)