Skip to content

string_tokenizer: free the UTF-8 copy in tokenize - #529

Closed
YeungKC wants to merge 1 commit into
mainfrom
string_tokenizer/free-utf8-copy
Closed

YeungKC wants to merge 1 commit into
mainfrom
string_tokenizer/free-utf8-copy

Conversation

@YeungKC

@YeungKC YeungKC commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Summary

tokenize() did not free the buffer from toNativeUtf8(), so each call leaked a copy of the input string. CFStringCreateWithCString copies the bytes, so the buffer is freed after the call.

+  final utf8 = string.toNativeUtf8();
   final cfString = _bindings.CFStringCreateWithCString(
     nullptr,
-    string.toNativeUtf8().cast(),
+    utf8.cast(),
     binding.CFStringBuiltInEncodings.kCFStringEncodingUTF8.value,
   );
+  malloc.free(utf8);

Evidence

Call tokenize() 200 times on a 1 MB string, then compare RSS (macOS):

  • Before: RSS +146 MB
    After: RSS -11 MB

tokenize('hello world 你好世界') returns [hello, world, 你, 好, 世界] before and after.

Merge Danger

Door: two-way

Blast Radius: small

No API change. CFStringCreateWithCString does not keep the pointer.

tokenize() did not free the buffer from toNativeUtf8(), so each call
leaked a copy of the input string. CFStringCreateWithCString copies
the bytes, so free the buffer after the call.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The buffer is freed with the matching allocator after its final use, with no blocking issues identified.

No open issues 🎉

What changed in this PR

Fixes a per-call native memory leak in string_tokenizer.tokenize() without changing its public API.

Changes:

  • Retains the temporary UTF-8 pointer and frees it after Core Foundation copies its contents.
File Description
packages/​string_tokenizer/​lib/​string_tokenizer.dart Frees the temporary UTF-8 allocation after string creation.

Review effort: Balanced

@YeungKC

YeungKC commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

#548 replaced string_tokenizer with word_segmenter and also fixed this UTF-8 buffer leak, so this PR is no longer necessary.

@YeungKC YeungKC closed this Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants