There are few information to detect the reason of your bug but here a little explanation of what stack and heap memories are:
stack memory is used by cpu to store all the local array or pointers needed to manage the returning of function calls (think of an interrupt that is served so the cpu must store the right memory address to recall when its routine terminate). when you use an rtos usually define a priori the amount of memory reserved for each task. if you don’t need an rtos the stack memory is all the free ram memory you leave in your program.
heap memory is a kind of reserved memory to manage the dynamic memory access. When you have memory space problem you can decide to use the amount of needed memory in sharing way calling a malloc() or calloc(). In such a way you request to get it for a short time and then release that amount calling free().
The size of that memory is always defined,
Hope could help!