<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Trap on operator new: Thoughts and Questions... in Kinetis Design Studio</title>
    <link>https://community.nxp.com/t5/Kinetis-Design-Studio/Trap-on-operator-new-Thoughts-and-Questions/m-p/375693#M1935</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;OK, so HEAP_SIZE in ProcessorExpert.ld is only 0x0400! OUCH!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But of course, that is an auto-generated file. If only I can figure out HOW to change that somewhere in PE? Still looking...&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 25 Jan 2015 20:43:54 GMT</pubDate>
    <dc:creator>tharonhall</dc:creator>
    <dc:date>2015-01-25T20:43:54Z</dc:date>
    <item>
      <title>Trap on operator new: Thoughts and Questions...</title>
      <link>https://community.nxp.com/t5/Kinetis-Design-Studio/Trap-on-operator-new-Thoughts-and-Questions/m-p/375692#M1934</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif;"&gt;So I am using a K22F with 512K Flash and 128K RAM. Therefore, I have LOTS of RAM and should not in any way run out of RAM for the relatively little code I have developed so far, assuming the RAM space is adequately allocated.&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif;"&gt;Given that Processor Expert (PE) basically creates a main.c and you have a limited space to place your code, and my code is actually in C++, I make a call from main in the user section to a main.cpp that then instantiates all the C++ code, including prepping threads to execute once the OSA (FreeRTOS flavor) scheduler kicks in.&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif;"&gt;All of that has been running great. However, as I added a little more "meat" to some of the lower level classes, I began getting a trap in the &lt;SPAN style="font-family: 'courier new', courier;"&gt;new&lt;/SPAN&gt; operator. Of course, you would suspect the new code, but the funny thing is that it allocates 4 classes before it barfs on the 5th one. It looks more like a possible memory allocation issue. Below is the code:&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;//--------------------&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;//Constructor&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;//--------------------&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;Model::Model()&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;{&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; //Provide access to the one instance&lt;/P&gt;&lt;P&gt;&amp;nbsp; theModel_p = this;&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; //Allocate the hardware support&lt;/P&gt;&lt;P&gt;&amp;nbsp; hw_p = new Hardware;&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; //Allocate the User Experience Managers&lt;/P&gt;&lt;P&gt;&amp;nbsp; for(int i=0; i &amp;lt; cfg_numHeads; i++)&lt;/P&gt;&lt;P&gt;&amp;nbsp; {&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; uxManagers_p[i] = new UxManager; //&amp;lt;---***Trap is here on 5th iteration***&lt;/P&gt;&lt;P&gt;&amp;nbsp; }&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; //Allocate the System Status&lt;/P&gt;&lt;P&gt;&amp;nbsp; sysStatus_p = new SysStatus;&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; //Allocate the communications&lt;/P&gt;&lt;P&gt;&amp;nbsp; comms_p = new Comm;&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now, this whole problem got me to thinking about something. PE is running its init before my code even runs, so there could be some black magic going on there. However, I would assume that any classes that are instantiated before the scheduler runs come out of the general C/C++ heap. However, I would assume that any memory allocations, including the new operator, will come out of the allocated FreeRTOS heap when executed from a thread. The begs the question:&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Should I continue to allocate my classes and OS elements prior to starting the OSA scheduler, or should I kick of a start up thread that then goes and instantiates all the classes from the FreeRTOS heap?&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Given my understanding that the new operator was not running out of the FreeRTOS heap I am not surprised, but bumping up the FreeRTOS heap allocation did nothing to address my issue.&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For what it's worth, here is the call stack when the trap fires:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="19131_19131.png"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/120148i13A76CC590B41061/image-size/large?v=v2&amp;amp;px=999" role="button" title="19131_19131.png" alt="19131_19131.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="new operator trap.png"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/49535iC6001F37BDC9C50B/image-size/large?v=v2&amp;amp;px=999" role="button" title="new operator trap.png" alt="new operator trap.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Any thoughts or insights would be most appreciated. Perhaps I simply need to increase the amount of data allocated to the C/C++ heap if anyone knows where to change that? I was up late working on this and I am not looking forward to pulling my hair out all afternoon this fine Sunday. &lt;SPAN aria-label="Sad" class="emoticon_sad emoticon-inline" style="height:16px;width:16px;"&gt;&lt;/SPAN&gt; Rest assured, I will be Googling this ad infinitum, but wanted to put this out, especially before our dear friends in Europe went to bed. &lt;SPAN aria-label="Wink" class="emoticon-inline emoticon_wink" style="height:16px;width:16px;"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;"I'm taking what they're giving 'cause I'm working through the weekend."&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 25 Jan 2015 20:11:16 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Design-Studio/Trap-on-operator-new-Thoughts-and-Questions/m-p/375692#M1934</guid>
      <dc:creator>tharonhall</dc:creator>
      <dc:date>2015-01-25T20:11:16Z</dc:date>
    </item>
    <item>
      <title>Re: Trap on operator new: Thoughts and Questions...</title>
      <link>https://community.nxp.com/t5/Kinetis-Design-Studio/Trap-on-operator-new-Thoughts-and-Questions/m-p/375693#M1935</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;OK, so HEAP_SIZE in ProcessorExpert.ld is only 0x0400! OUCH!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But of course, that is an auto-generated file. If only I can figure out HOW to change that somewhere in PE? Still looking...&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 25 Jan 2015 20:43:54 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Design-Studio/Trap-on-operator-new-Thoughts-and-Questions/m-p/375693#M1935</guid>
      <dc:creator>tharonhall</dc:creator>
      <dc:date>2015-01-25T20:43:54Z</dc:date>
    </item>
    <item>
      <title>Re: Trap on operator new: Thoughts and Questions...</title>
      <link>https://community.nxp.com/t5/Kinetis-Design-Studio/Trap-on-operator-new-Thoughts-and-Questions/m-p/375694#M1936</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Bingo! Under Processors in PE Panel select the CPU, then Component Inspector, Build Options tab, then the Generate linker file tab. :smileyhappy:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However, the code is trapping in exactly the same way even though I tripled the stack size and increased the heap size by several times. :smileysad:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any thoughts on another approach to debugging this? Is there a compelling argument for doing this using from within an OSA/FreeRTOS thread instead and using the OS heap?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;???- Any tricks to knowing exactly what the new operator is barfing on? Since that is in a library, I don't know that I have visibility into what is going on there. :smileysad:&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 25 Jan 2015 21:02:46 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Design-Studio/Trap-on-operator-new-Thoughts-and-Questions/m-p/375694#M1936</guid>
      <dc:creator>tharonhall</dc:creator>
      <dc:date>2015-01-25T21:02:46Z</dc:date>
    </item>
    <item>
      <title>Re: Trap on operator new: Thoughts and Questions...</title>
      <link>https://community.nxp.com/t5/Kinetis-Design-Studio/Trap-on-operator-new-Thoughts-and-Questions/m-p/375695#M1937</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;OK! Finally. It looks like in my effort to address the problem initially, I upped the OSA/FreeRTOS heap without also upping the linker heap. Therefore, there really wasn't enough memory for FreeRTOS to pull it's own heap from the general heap. :smileyhappy: While the small-ish heap may have been the initial heap, not increasing the C heap along with the FreeRTOS heap probably prolonged the pain. :smileysad:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;??? - Now if anyone has a compelling reason to consider instantiating my classes from OSA/FreeRTOS's heap from a startup thread, vs. doing it initially using the C heap, I am all ears. Perhaps debugging this issue would have been better, for example? I don't necessarily want to create another thread if there is no compelling reason to otherwise.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 25 Jan 2015 21:18:50 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Design-Studio/Trap-on-operator-new-Thoughts-and-Questions/m-p/375695#M1937</guid>
      <dc:creator>tharonhall</dc:creator>
      <dc:date>2015-01-25T21:18:50Z</dc:date>
    </item>
    <item>
      <title>Re: Trap on operator new: Thoughts and Questions...</title>
      <link>https://community.nxp.com/t5/Kinetis-Design-Studio/Trap-on-operator-new-Thoughts-and-Questions/m-p/375696#M1938</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I guess I am the only person working on Sunday, so it looks like I am talking to myself. :smileywink:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So as I think about it, it probably doesn't matter whether or not the &lt;SPAN style="text-decoration: underline;"&gt;new&lt;/SPAN&gt; operator is used from within a thread or not, since it will go to the same library regardless, correct? I can't see the OS creating some kind of hook to redirect the operation to its heap, or am I missing something?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have my code working now but ultimately need to tune my memory allocations to match my usage.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 25 Jan 2015 22:14:31 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Design-Studio/Trap-on-operator-new-Thoughts-and-Questions/m-p/375696#M1938</guid>
      <dc:creator>tharonhall</dc:creator>
      <dc:date>2015-01-25T22:14:31Z</dc:date>
    </item>
  </channel>
</rss>

