From 424fab2e40db52d8f24604e6dfd11ad880c930c5 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Fri, 12 Dec 2025 04:08:01 +0000 Subject: [PATCH] Weird bug in draw_tree Seems near the 100,000 node mark, the vector craps itself? I keep getting "heap use after free" errors here when trying to access the allocator vector. Disabling fsan just leads to a segment fault near the same point. I think we might need to introduce our own vector :) --- src/main.cpp | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index b98da12..6f53a7c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -52,7 +52,7 @@ constexpr u64 clamp_to_width(const DrawState &ds, f64 val) (val - ds.bounds.lower_val); } -void draw_tree(const DrawState &ds) +void draw_tree(const DrawState &ds, const State &state) { // Number line DrawLine(0, HEIGHT / 2, WIDTH, HEIGHT / 2, WHITE); @@ -63,18 +63,29 @@ void draw_tree(const DrawState &ds) DrawLine(lower_x, LINE_TOP, lower_x, LINE_BOTTOM, WHITE); DrawLine(upper_x, LINE_TOP, upper_x, LINE_BOTTOM, WHITE); - std::stack stack; - stack.push(ds.state.allocator.get_val(0)); + std::stack> stack; + cw::node::Node n = state.allocator.get_val(0); + stack.push(std::make_pair(0, n.value.norm)); while (!stack.empty()) { - auto n = stack.top(); + u64 node_index; + f64 norm; + std::tie(node_index, norm) = stack.top(); stack.pop(); - u64 x = clamp_to_width(ds, n.value.norm); + u64 x = clamp_to_width(ds, norm); DrawLine(x, LINE_TOP, x, LINE_BOTTOM, RED); + + cw::node::Node n = state.allocator.get_val(node_index); if (n.left >= 0) - stack.push(ds.state.allocator.get_val(n.left)); + { + cw::node::Node left = state.allocator.get_val(n.left); + stack.push(std::make_pair(n.left, left.value.norm)); + } if (n.right >= 0) - stack.push(ds.state.allocator.get_val(n.right)); + { + cw::node::Node right = state.allocator.get_val(n.right); + stack.push(std::make_pair(n.right, right.value.norm)); + } } } @@ -145,7 +156,7 @@ int main(void) ClearBackground(BLACK); BeginDrawing(); - draw_tree(draw_state); + draw_tree(draw_state, state); DrawText(format_str.c_str(), WIDTH / 2 - format_str_width / 2, HEIGHT / 8, FONT_SIZE, WHITE); EndDrawing();