# Memory leak with BufMap

**URL:** <https://ziggit.dev/t/memory-leak-with-bufmap/18132>\
**Category:** Help\
**Tags:** memory-management, standard-library\
**Created:** [October 28, 2026, 8:51pm UTC](https://ziggit.dev/t/memory-leak-with-bufmap/18132 "2026-10-28T20:51:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![exfalsoquodlibet](https://ziggit.dev/letter_avatar_proxy/v4/letter/e/bbce88/32.png) [@exfalsoquodlibet](https://ziggit.dev/u/exfalsoquodlibet)\
**Post date:** [October 28, 2026, 8:51pm UTC](https://ziggit.dev/t/memory-leak-with-bufmap/18132/1 "2026-10-28T20:51:52Z")

</div>

Hi 🙂

My understanding of the documentation of `BufMap` is that the `deinit` should take care of freeing all the entries (keys and values) along with the backing storage (it’s also my understanding of its implementation). Now the following test reports a memory leak:

```zig
test "simple test" {
    const gpa = std.testing.allocator;
    var map = std.BufMap.init(gpa);
    errdefer map.deinit();
    try map.put("alice", "bob");
}

```

What did I miss?  
I’m on version 0.17.0.

---

<div class="post-metadata">

**Author:** ![biom4st3r](https://ziggit.dev/user_avatar/ziggit.dev/biom4st3r/32/4342_2.png) [@biom4st3r](https://ziggit.dev/u/biom4st3r)\
**Post date:** [October 28, 2026, 9:09pm UTC](https://ziggit.dev/t/memory-leak-with-bufmap/18132/2 "2026-10-28T21:09:14Z")

</div>

Youre only calling deinit when an err occurs. Use `defer` instead of `errdefer`

---

<div class="post-metadata">

**Author:** ![exfalsoquodlibet](https://ziggit.dev/letter_avatar_proxy/v4/letter/e/bbce88/32.png) [@exfalsoquodlibet](https://ziggit.dev/u/exfalsoquodlibet)\
**Post date:** [October 28, 2026, 9:16pm UTC](https://ziggit.dev/t/memory-leak-with-bufmap/18132/3 "2026-10-28T21:16:01Z")

</div>

arf, I got the couple `defer` / `errdefer` wrong… I should re-read the manual…
