# Weird SafeAllocator issue causing stack overflow

**URL:** <https://ziggit.dev/t/weird-safeallocator-issue-causing-stack-overflow/18137>\
**Category:** Help\
**Tags:** memory-management, c, standard-library\
**Created:** [October 29, 2026, 9:32am UTC](https://ziggit.dev/t/weird-safeallocator-issue-causing-stack-overflow/18137 "2026-10-29T09:32:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![senjosei](https://ziggit.dev/user_avatar/ziggit.dev/senjosei/32/7498_2.png) [@senjosei](https://ziggit.dev/u/senjosei)\
**Post date:** [October 29, 2026, 9:32am UTC](https://ziggit.dev/t/weird-safeallocator-issue-causing-stack-overflow/18137/1 "2026-10-29T09:32:54Z")

</div>

I encountered this weird issue and is still trying to understand it to make a minimal reproducable repo but writing it here incase im dumm and the issue is something else. Im making this [What's everybody working on? (October edition) - #71 by senjosei](https://ziggit.dev/t/whats-everybody-working-on-october-edition/17770/71) and one of the components is [GitHub - azahar-emu/dynarmic · GitHub](https://github.com/azahar-emu/dynarmic). Now to use it u gotta make a “env” obj with callbacks for memory stuff and then make a “jit” obj to run it. So i exported something like this

```c++
typedef struct Dynarmic64Callbacks {
    void* context;

    u8 (*memory_read_8)(void* context, u64 vaddr);
    u16 (*memory_read_16)(void* context, u64 vaddr);
    u32 (*memory_read_32)(void* context, u64 vaddr);
    // other cbs
}
//extern c methods
DynarmicA64Env* create_env_64(const Dynarmic64Callbacks* callbacks) {
    auto tmp = std::unique_ptr<DynarmicA64Env>(new (std::nothrow) DynarmicA64Env(*callbacks));
    return tmp.release();
}
// other methods
//the env class
class DynarmicA64Env final : public Dynarmic::A64::UserCallbacks {
public:
    using VAddr = Dynarmic::A64::VAddr;
    using Exception = Dynarmic::A64::Exception;

    explicit DynarmicA64Env(const Dynarmic64Callbacks& callbacks): callbacks(callbacks) {}

    u8 MemoryRead8(VAddr vaddr) override {
        return callbacks.memory_read_8(callbacks.context, vaddr);
    }
// etc...
}

```

Then on the zig side i have

```zig
pub const DynarmicCallbacks = extern struct {
    context: ?*anyopaque,
    memory_read_8: ?*const fn (?*anyopaque, u64) callconv(.c) u8,
    memory_read_16: ?*const fn (?*anyopaque, u64) callconv(.c) u16,
    memory_read_32: ?*const fn (?*anyopaque, u64) callconv(.c) u32,
    //etc
};
pub extern fn create_env_64(*const DynarmicCallbacks) callconv(.c) ?*DynarmicA64Env;
pub extern fn destroy_env_64(*DynarmicA64Env) callconv(.c) void;
// example one of the cbs fn i set in the struct then pass to cpp side
fn read_8(callbacks: ?*anyopaque, address: u64) callconv(.c) u8 {
    const emu = emulatorOf(callbacks); // just ptr casts
    const bytes = emu.space.bytes(address, 1) orelse {
        emu.fault = error.ReadFault;
        return 0;
    };
    return bytes[0];
}

```

Now heres the weird part. One of the exported callbacks is `call_svc: ?*const fn (?*anyopaque, u32) callconv(.c) void` which then calls the host implemented functions and stuff so somewhere in there its calling `gpa.free`. I’ve been using juicy main and running on debug mode so it defaults to `SafeAllocator`, it just crashes with a stack overflow error. Im not familiar with debugging in zig but i did attach lldb and bt showed this.

```zig
// infinite
frame #16602: 0x00007ffbd39ff2c9 ntdll.dll`RtlWow64GetCurrentCpuArea + 2569
frame #16603: 0x00007ffbd3b436de ntdll.dll`KiUserExceptionDispatcher + 46
frame #16604: 0x00007ffbd3a0293e ntdll.dll`RtlVirtualUnwind2 + 6830
frame #16605: 0x00007ffbd3a01f59 ntdll.dll`RtlVirtualUnwind2 + 4297
frame #16606: 0x00007ffbd39ff2c9 ntdll.dll`RtlWow64GetCurrentCpuArea + 2569
frame #16607: 0x00007ffbd3b436de ntdll.dll`KiUserExceptionDispatcher + 46
frame #16608: 0x00007ffbd3a0293e ntdll.dll`RtlVirtualUnwind2 + 6830
frame #16609: 0x00007ffbd3a01f59 ntdll.dll`RtlVirtualUnwind2 + 4297
frame #16610: 0x00007ffbd3a035a4 ntdll.dll`RtlVirtualUnwind + 484
frame #16611: 0x00007ffbd1e58008 kernel32.dll`RtlVirtualUnwind + 56
frame #16612: 0x00007ff7d43fc6f1 zigote.exe`main at Windows.zig:187:43
frame #16613: 0x00007ff7d43fb660 zigote.exe`main at debug.zig:1099:48
frame #16614: 0x00007ff7d451881e zigote.exe`wmemcmp at debug.zig:699:83
frame #16615: 0x00007ff7d45182a6 zigote.exe`wmemcmp at SafeAllocator.zig:877:49
frame #16616: 0x00007ff7d451e4a4 zigote.exe`wmemcmp at SafeAllocator.zig:1139:22
frame #16617: 0x00007ff7d4502c1b zigote.exe`main at Allocator.zig:476:17
frame #16618: 0x00007ff7d4490798 zigote.exe`main at gl.zig:542:38
frame #16619: 0x00007ff7d44b0c5c zigote.exe`main at emu.zig:341:38
frame #16620: 0x00007ff7d44b056d zigote.exe`main at emu.zig:548:23
frame #16621: 0x00007ff7d4548aa4 zigote.exe`void DynarmicA64Env::CallSVC(this=0x00000273288813c0, swi=67) at c_api.cpp:168

```

BUT if i then compile it with `ReleaseFast` it runs fine. Then i tried going to `SafeAllocator.zig` and in the option struct. i changed the `is_debug` to false

```zig
pub const Options = struct {
    const is_debug = false; //was @import("builtin").mode == .debug
    const page_size_log2 = @max(math.log2_int(usize, std.heap.page_size_max), 8);

```

and it works with the safeallocator in debug mode. So yea im still trying to figure out why but maybe someone here knows whats causing it. On zig 0.18.0-dev.738+ae9a8acc7

---

<div class="post-metadata">

**Author:** ![mmirj](https://ziggit.dev/user_avatar/ziggit.dev/mmirj/32/10463_2.png) [@mmirj](https://ziggit.dev/u/mmirj)\
**Post date:** [October 30, 2026, 12:27am UTC](https://ziggit.dev/t/weird-safeallocator-issue-causing-stack-overflow/18137/2 "2026-10-30T00:27:11Z")

</div>

Hmm, how about turning the stack traces off for your allocator?

```zig
var safe: std.heap.SafeAllocator = .init(std.heap.page_allocator, .{ .stack_trace_frames = 0 });
const gpa = safe.allocator();

```
