Skip to content

Encoder: Segfault when doing (de)allocation between init/uninit calls #975

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
nat3Github opened this issue Mar 30, 2025 · 4 comments
Open

Comments

@nat3Github
Copy link

because im writing zig i will write pseudocode:

this works:
ma_encoder_init_file(...)
ma_encoder_uninit(...)

this segfaults:
ma_encoder_init_file(...)
some_allocator_allocate_some_bytes()
ma_encoder_uninit(...)
some_allocator_free_some_bytes()

errors with a segfault which i will attach here after.
Q: does this hint at a bug triggering this segfault or is it forbidden to do any allocation / dealloction between the init/uninit calls of encoder?

Segmentation fault at address 0x52bddff160
D:\Programming\Zig\lib\musicfiles\miniaudio\extras\miniaudio_split\manual patch\miniaudio.c:54630:0: 0x7ff6343e4ee9 in ma_encoder__internal_on_seek_wav (miniaudio.obj)
result = pEncoder->onSeek(pEncoder, offset, (origin == ma_dr_wav_seek_origin_start) ? ma_seek_origin_start : ma_seek_origin_current);

D:\Programming\Zig\lib\musicfiles\miniaudio\extras\miniaudio_split\manual patch\miniaudio.c:69301:0: 0x7ff634273029 in ma_dr_wav_uninit (miniaudio.obj)
if (pWav->onSeek(pWav->pUserData, 4, ma_dr_wav_seek_origin_start)) {

D:\Programming\Zig\lib\musicfiles\miniaudio\extras\miniaudio_split\manual patch\miniaudio.c:54684:0: 0x7ff6342addfb in ma_encoder__on_uninit_wav (miniaudio.obj)
ma_dr_wav_uninit(pWav);

D:\Programming\Zig\lib\musicfiles\miniaudio\extras\miniaudio_split\manual patch\miniaudio.c:54887:0: 0x7ff6342aea9e in ma_encoder_uninit (miniaudio.obj)
pEncoder->onUninit(pEncoder);

D:\Programming\Zig\lib\musicfiles\src\AudioNode\fileio.zig:437:32: 0x7ff6345da78b in deinit (test.exe.obj)
c.ma_encoder_uninit(&self.enc);

@mackron
Copy link
Owner

mackron commented Mar 31, 2025

With your some_allocator_allocate_some_bytes() call, do you mean you're allocating from a miniaudio ma_allocation_callbacks object? If so, are you using the same callbacks in the config you passed into ma_encoder_init_file()?

miniaudio has no restrictions on your memory allocations. It uses allocation callbacks, which if you don't provide yourself will use malloc/realloc/free internally. There's nothing unusual going on within miniaudio with it's memory management. All of your allocations should be totally independent of each other, unless you are passing in your own allocation callbacks in which case you're responsible for allocation correctness.

I don't have an explanation for this one. Maybe there is some kind of memory overrun or something happening, but that seems exceptionally unlikely because I've just looked now and I can't see any memory allocations happening in ma_encoder_init_file() nor ma_encoder_uninit(), and I don't think allocations happen at all when writing out directly to a file like you're doing here.

@nat3Github
Copy link
Author

nat3Github commented Mar 31, 2025

with my some_allocator_allocate_some_bytes() i was referring to an allocator separate from miniaudio. specifically i had this issue with the std.testing.allocator from the zig library.

thank you for looking into this, this is definitely weird!

@mackron
Copy link
Owner

mackron commented Mar 31, 2025

OK, I don't think this is an issue with miniaudio then. If you don't specify your own allocation callbacks, which sounds like is the case for you, miniaudio will always use C's malloc/free. The Zig allocation stuff has nothing to do with miniaudio so I don't see how that can be affecting things.

Are you sure this isn't an issue with the Zig bindings?

@nat3Github
Copy link
Author

nat3Github commented Apr 1, 2025

could be an issue with zig or maybe an issue of malloc / testing allocator causing some bad (side) effects that i don't know of (the testing allocator detects memory leaks and therefore tracks stuff).

i don't think it's an issue with the bindings because they are auto translated from the header, merely call the compiled c code (compilation via clang) and they do work by themselves.

it's even weirder if no allocation is happening in the miniaudio calls.

i will check if the issue persists using other allocators.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants