libghostty: add ghostty_free for cross-runtime memory safety

On Windows, Zig's built-in libc and MSVC's CRT maintain separate
heaps, so calling free() on memory allocated by the library causes
undefined behavior. Add ghostty_free() that frees through the same
allocator that performed the allocation, making it safe on all
platforms.

Update format_alloc docs and all examples to use ghostty_free()
instead of free().

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Alessandro De Blasis
2026-03-23 16:54:05 +01:00
parent 1213dacd5b
commit c1e616c6cd
7 changed files with 50 additions and 7 deletions

View File

@@ -45,7 +45,7 @@ int main() {
fwrite(buf, 1, len, stdout);
printf("\n");
free(buf);
ghostty_free(NULL, buf, len);
ghostty_formatter_free(formatter);
ghostty_terminal_free(terminal);
return 0;

View File

@@ -45,7 +45,7 @@ int main() {
fwrite(buf, 1, len, stdout);
printf("\n");
free(buf);
ghostty_free(NULL, buf, len);
ghostty_formatter_free(formatter);
ghostty_terminal_free(terminal);
return 0;

View File

@@ -56,7 +56,7 @@ int main() {
printf("\n");
// Clean up
free(buf);
ghostty_free(NULL, buf, len);
ghostty_formatter_free(formatter);
ghostty_terminal_free(terminal);
return 0;