5 Replies Latest reply on May 20, 2019 4:17 AM by LePo_1062026

    Out of Heap exception




      I am using Cypress PSoC 5LP CY8C5888LT*-LP097  with PSOC 4.2


      Flash used: 46204 of 262144 bytes (17.6%).

      SRAM used: 60465 of 65536 bytes (92.3%). Stack: 2048 bytes. Heap: 2048 bytes.


      When I enable speed optimization I get the following exception during runtime



      I tried to Increase the Heap to 4K or 6 K but it dosn't help


      How to overcome this problem?


      Hossam Alzomor

        • 1. Re: Out of Heap exception



          Just to make sure.  You changed the Heap allocation to 4K or 6K using:


          You get a runtime error not a compile/linker error.  Are you using FreeRTOS with dynamic memory allocations?


          I way to help debug where in the code there might be an issue is when you break in the IntDefaultHandler ISR look at the Call stack.  This is the debugger's best guess where you have been in the code because these are the PC values stored on the stack during a call or interrupt.

          Sometimes I remember getting into this IntDefaultHandler when I was using FreeRTOS and didn't allocate enough stack for the task.  I used the Call Stack recommendation above to find the offending task and allocate more stack during the task initialization call.


          Another suggestion is to reduce your memory requirements used by your application.   I noticed you are using 92.3% of available RAM.  Wow!  Temporarily commenting out a high RAM use function might prevent the IntDefaultHandler issue,


          Let me know if any of the above suggestions work out.



          • 2. Re: Out of Heap exception

            Thanks len,


            I am modifying the stack in the right place

            I am not using freertos, and not using memalloc


            Dose optimized code requires such much heap/stack size?



            Best Regards

            Hossam Alzomor

            • 3. Re: Out of Heap exception



              Optimized code will generally lower FLASH and RAM requirements unless optimized for speed.


              Try my debugging suggestion with the Call Stack.


              If that doesn't work, you may have to share your project.



              • 4. Re: Out of Heap exception



                I am optimizing for speed.


                Here's a snapshot from the stack and registers just after the exception

                optimized after problem.PNG

                and here's just before the exception

                optimized before problem.PNG

                and here's the line that causes the problem



                line cause problem.PNG

                Do you have any recommendation?


                Hossam Alzomor

                • 5. Re: Out of Heap exception

                  Hossam Alzomor,


                  Nothing looks obvious however check your code for the following:

                  • uint8 *rxdata points to an array of at least two bytes.  If you only have a variable with one byte the line dumping the incoming SPI data into rxdata[1] will overwrite unknown data. This unknown data could be stack or other RAM with pointer information.
                  • You have a while statement that loops until at least one byte is available in the SPIM_1 Rx buffer.  You  might want to change the statement to: while(SPIM_1_GetRxBufferSize() <2);   Since the next three statements are assuming that at least two bytes are being read from the SPI, you might want to make sure at least two bytes have been received.  Your code only assumes one byte is available.

                  Hope this helps.