Does a stack allocation through _malloca trigger an alloc hook set through _CrtSetAllocHook

160 Views Asked by At

For a realtime audio signal processing application, we want to make sure that no heap memory allocations are performed from within the realtime threads. As an internal debugging tool used during development, we set up an heap allocation hook function through _CrtSetAllocHook that checks the thread ID of the allocating thread and asserts if it is a realtime thread.

In some parts of our codebase we now use _malloca / _freea to temporarily create stack buffers < 400 bytes. According to the Microsoft documentations, _malloca performs a heap allocation instead of a stack allocation when the number of allocated bytes is greater than the value defined by _ALLOCA_S_THRESHOLD. _ALLOCA_S_THRESHOLD is currently set to 1024.

We now run into the heap allocation assertion when _malloca allocates small stack buffers under the threshold, e.g. if no heap allocation should occur. I didn't find any information if the allocation hook set through _CrtSetAllocHook is also triggered in case _malloca decides to perform a stack allocation, but I get the feeling that this might be the case.

So first questions: Does anyone find any official documentation on the behaviour that can be expected here? Second question: If the alloc hook is invoked for both stack and heap allocations as I assume, how should we then figure out which kind of allocation was performed to only trigger the assertion for heap allocations?

0

There are 0 best solutions below