fix: resolve memory leak in Option.getText() by using page arena

This commit is contained in:
sjhddh
2026-03-14 06:50:26 +00:00
parent 535128da71
commit c4176a282f
2 changed files with 4 additions and 5 deletions

View File

@@ -231,7 +231,7 @@ fn extractSelectOptions(node: *Node, page: *Page, arena: std.mem.Allocator) ![]O
if (child.is(Element)) |el| { if (child.is(Element)) |el| {
if (el.getTag() == .option) { if (el.getTag() == .option) {
if (el.is(Element.Html.Option)) |opt| { if (el.is(Element.Html.Option)) |opt| {
const text = opt.getText(); const text = opt.getText(page);
const value = opt.getValue(page); const value = opt.getValue(page);
const selected = opt.getSelected(); const selected = opt.getSelected();
try options.append(arena, .{ .text = text, .value = value, .selected = selected }); try options.append(arena, .{ .text = text, .value = value, .selected = selected });
@@ -240,7 +240,7 @@ fn extractSelectOptions(node: *Node, page: *Page, arena: std.mem.Allocator) ![]O
var group_it = child.childrenIterator(); var group_it = child.childrenIterator();
while (group_it.next()) |group_child| { while (group_it.next()) |group_child| {
if (group_child.is(Element.Html.Option)) |opt| { if (group_child.is(Element.Html.Option)) |opt| {
const text = opt.getText(); const text = opt.getText(page);
const value = opt.getValue(page); const value = opt.getValue(page);
const selected = opt.getSelected(); const selected = opt.getSelected();
try options.append(arena, .{ .text = text, .value = value, .selected = selected }); try options.append(arena, .{ .text = text, .value = value, .selected = selected });

View File

@@ -61,10 +61,9 @@ pub fn setValue(self: *Option, value: []const u8, page: *Page) !void {
self._value = owned; self._value = owned;
} }
pub fn getText(self: *const Option) []const u8 { pub fn getText(self: *const Option, page: *Page) []const u8 {
const node: *Node = @constCast(self.asConstElement().asConstNode()); const node: *Node = @constCast(self.asConstElement().asConstNode());
const allocator = std.heap.page_allocator; // TODO: use proper allocator return node.getTextContentAlloc(page.call_arena) catch "";
return node.getTextContentAlloc(allocator) catch "";
} }
pub fn setText(self: *Option, value: []const u8, page: *Page) !void { pub fn setText(self: *Option, value: []const u8, page: *Page) !void {