<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>An issue with memory allocation in NRF52832 when working with JSON files</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/102436/an-issue-with-memory-allocation-in-nrf52832-when-working-with-json-files</link><description>Hello, 
 I am using the NRF52832 processor on a custom board to collect data from a sensor and send it to a mobile app using BLE and NUS service in JSON format. However, after a few minutes, the transmission stops due to memory allocation failure. 
 Here</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Sun, 06 Aug 2023 09:19:53 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/102436/an-issue-with-memory-allocation-in-nrf52832-when-working-with-json-files" /><item><title>RE: An issue with memory allocation in NRF52832 when working with JSON files</title><link>https://devzone.nordicsemi.com/thread/440007?ContentTypeID=1</link><pubDate>Sun, 06 Aug 2023 09:19:53 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:569afbb8-f92f-485f-9797-865fdb9c1d76</guid><dc:creator>ZaQa</dc:creator><description>&lt;p&gt;Hi&amp;nbsp;&lt;span&gt;H&amp;aring;kon,&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
[quote userid="2115" url="~/f/nordic-q-a/102436/an-issue-with-memory-allocation-in-nrf52832-when-working-with-json-files/439719"]try switching to static allocation for everything else except cJSON_* calls.[/quote]
&lt;p&gt;&lt;span&gt;&lt;br /&gt;I have been addressing this problem since then and I am pleased to report that it has been resolved successfully. Based on your previous comments, I have made changes to the memory allocation and replaced all the static cJSON calls with dynamic memory allocation. The memory leakage issue has been eliminated, and everything is functioning correctly.&amp;nbsp;&lt;br /&gt;&lt;br /&gt;Thank you very much for taking the time to guide me. I truly appreciate your patience.&lt;br /&gt;&lt;br /&gt;Warmest regards,&lt;br /&gt;&lt;br /&gt;ZaQa&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: An issue with memory allocation in NRF52832 when working with JSON files</title><link>https://devzone.nordicsemi.com/thread/439719?ContentTypeID=1</link><pubDate>Thu, 03 Aug 2023 13:34:53 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0e79036a-ead6-492b-af66-5c4d74c4b3ff</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;For instance: for each malloc, print a &amp;quot;+&amp;quot;, and for each free call, print a &amp;quot;-&amp;quot;.&lt;/p&gt;
&lt;p&gt;That list shows your firmware is leaking memory, try switching to static allocation for everything else except cJSON_* calls.&lt;/p&gt;
&lt;p&gt;Also, I would recommend that you add some waiting logic (wait for the callback with evt&amp;nbsp;&lt;span&gt;BLE_GATTS_EVT_HVN_TX_COMPLETE)&lt;/span&gt; after the &lt;span&gt;ble_nus_data_send() call, as this is a non-blocking function.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: An issue with memory allocation in NRF52832 when working with JSON files</title><link>https://devzone.nordicsemi.com/thread/439710?ContentTypeID=1</link><pubDate>Thu, 03 Aug 2023 12:58:01 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:89cd2621-873a-4ae6-9b32-0bc37faee613</guid><dc:creator>ZaQa</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
[quote userid="2115" url="~/f/nordic-q-a/102436/an-issue-with-memory-allocation-in-nrf52832-when-working-with-json-files/439664"]This is the only place where you actually free the allocated memory, thus the HEAP goes up again. You need to adjust your heap size and/or handle your timing accordingly.[/quote]
&lt;p&gt;I tried it again, but this time I increased the heap size to 16KB. However, I obtained the same result:&lt;/p&gt;
&lt;p&gt;allocated address : 2000955c&lt;/p&gt;
&lt;p&gt;allocated address : 2000949c&lt;/p&gt;
&lt;p&gt;allocated address : 2000945c&lt;/p&gt;
&lt;p&gt;allocated address : 2000934c&lt;/p&gt;
&lt;p&gt;allocated address : 2000930c&lt;/p&gt;
&lt;p&gt;allocated address : 200092dc&lt;/p&gt;
&lt;p&gt;allocated address : 2000920c&lt;/p&gt;
&lt;p&gt;allocated address : 200091dc&lt;/p&gt;
&lt;p&gt;allocated address : 2000915c&lt;/p&gt;
&lt;p&gt;allocated address : 2000910c&lt;/p&gt;
&lt;p&gt;allocated address : 2000908c&lt;/p&gt;
&lt;p&gt;allocated address : 2000904c&lt;/p&gt;
&lt;p&gt;allocated address : 2000901c&lt;/p&gt;
&lt;p&gt;allocated address : 20008fcc&lt;/p&gt;
&lt;p&gt;allocated address : 20008f1c&lt;/p&gt;
&lt;p&gt;allocated address : 20008eec&lt;/p&gt;
&lt;p&gt;allocated address : 20008edc&lt;/p&gt;
&lt;p&gt;allocated address : 20008eac&lt;/p&gt;
&lt;p&gt;allocated address : 20008e7c&lt;/p&gt;
&lt;p&gt;allocated address : 20008e6c&lt;/p&gt;
&lt;p&gt;allocated address : 20008e3c&lt;/p&gt;
&lt;p&gt;allocated address : 20008dbc&lt;/p&gt;
&lt;p&gt;allocated address : 20008d7c&lt;/p&gt;
&lt;p&gt;allocated address : 20008d0c&lt;/p&gt;
&lt;p&gt;allocated address : 20008c8c&lt;/p&gt;
&lt;p&gt;allocated address : 20008c2c&lt;/p&gt;
&lt;p&gt;allocated address : 20008bfc&lt;/p&gt;
&lt;p&gt;allocated address : 20008bec&lt;/p&gt;
&lt;p&gt;allocated address : 20008bbc&lt;/p&gt;
&lt;p&gt;allocated address : 20008b3c&lt;/p&gt;
&lt;p&gt;allocated address : 20008aec&lt;/p&gt;
&lt;p&gt;allocated address : 20008adc&lt;/p&gt;
&lt;p&gt;allocated address : 20008acc&lt;/p&gt;
&lt;p&gt;allocated address : 20008abc&lt;/p&gt;
&lt;p&gt;allocated address : 20008aac&lt;/p&gt;
&lt;p&gt;allocated address : 20008a9c&lt;/p&gt;
&lt;p&gt;allocated address : 20008a8c&lt;/p&gt;
&lt;p&gt;allocated address : 20008a7c&lt;/p&gt;
&lt;p&gt;allocated address : 20008a6c&lt;/p&gt;
&lt;p&gt;allocated address : 20008a5c&lt;/p&gt;
&lt;p&gt;allocated address : 20008a4c&lt;/p&gt;
&lt;p&gt;allocated address : 200089cc&lt;/p&gt;
&lt;p&gt;allocated address : 2000898c&lt;/p&gt;
&lt;p&gt;allocated address : 2000895c&lt;/p&gt;
&lt;p&gt;allocated address : 2000892c&lt;/p&gt;
&lt;p&gt;allocated address : 2000891c&lt;/p&gt;
&lt;p&gt;allocated address : 200088ec&lt;/p&gt;
&lt;p&gt;allocated address : 200088bc&lt;/p&gt;
&lt;p&gt;allocated address : 2000889c&lt;/p&gt;
&lt;p&gt;allocated address : 2000888c&lt;/p&gt;
&lt;p&gt;allocated address : 2000887c&lt;/p&gt;
&lt;p&gt;allocated address : 2000886c&lt;/p&gt;
&lt;p&gt;allocated address : 2000885c&lt;/p&gt;
&lt;p&gt;allocated address : 2000884c&lt;/p&gt;
&lt;p&gt;allocated address : 2000883c&lt;/p&gt;
&lt;p&gt;allocated address : 2000882c&lt;/p&gt;
&lt;p&gt;allocated address : 2000881c&lt;/p&gt;
&lt;p&gt;allocated address : 2000880c&lt;/p&gt;
&lt;p&gt;allocated address : 200087fc&lt;/p&gt;
&lt;p&gt;allocated address : 200087ec&lt;/p&gt;
&lt;p&gt;allocated address : 2000879c&lt;/p&gt;
&lt;p&gt;allocated address : 200086ec&lt;/p&gt;
&lt;p&gt;allocated address : 2000861c&lt;/p&gt;
&lt;p&gt;allocated address : 2000852c&lt;/p&gt;
&lt;p&gt;allocated address : 200084bc&lt;/p&gt;
&lt;p&gt;allocated address : 200083cc&lt;/p&gt;
&lt;p&gt;allocated address : 2000837c&lt;/p&gt;
&lt;p&gt;allocated address : 200082fc&lt;/p&gt;
&lt;p&gt;allocated address : 200082bc&lt;/p&gt;
&lt;p&gt;allocated address : 2000823c&lt;/p&gt;
&lt;p&gt;allocated address : 2000818c&lt;/p&gt;
&lt;p&gt;allocated address : 2000810c&lt;/p&gt;
&lt;p&gt;allocated address : 200080cc&lt;/p&gt;
&lt;p&gt;allocated address : 20007ffc&lt;/p&gt;
&lt;p&gt;allocated address : 20007fac&lt;/p&gt;
&lt;p&gt;allocated address : 20007f6c&lt;/p&gt;
&lt;p&gt;allocated address : 20007f3c&lt;/p&gt;
&lt;p&gt;allocated address : 20007f0c&lt;/p&gt;
&lt;p&gt;allocated address : 20007edc&lt;/p&gt;
&lt;p&gt;allocated address : 20007e9c&lt;/p&gt;
&lt;p&gt;allocated address : 20007e4c&lt;/p&gt;
&lt;p&gt;allocated address : 20007dac&lt;/p&gt;
&lt;p&gt;allocated address : 20007d4c&lt;/p&gt;
&lt;p&gt;allocated address : 20007cfc&lt;/p&gt;
&lt;p&gt;allocated address : 20007ccc&lt;/p&gt;
&lt;p&gt;allocated address : 20007c4c&lt;/p&gt;
&lt;p&gt;allocated address : 20007c1c&lt;/p&gt;
&lt;p&gt;allocated address : 20007b9c&lt;/p&gt;
&lt;p&gt;allocated address : 20007a7c&lt;/p&gt;
&lt;p&gt;allocated address : 20007a4c&lt;/p&gt;
&lt;p&gt;allocated address : 200079bc&lt;/p&gt;
&lt;p&gt;allocated address : 2000797c&lt;/p&gt;
&lt;p&gt;allocated address : 2000791c&lt;/p&gt;
&lt;p&gt;allocated address : 200078bc&lt;/p&gt;
&lt;p&gt;allocated address : 200077ec&lt;/p&gt;
&lt;p&gt;allocated address : 200077bc&lt;/p&gt;
&lt;p&gt;allocated address : 200077ac&lt;/p&gt;
&lt;p&gt;allocated address : 2000779c&lt;/p&gt;
&lt;p&gt;allocated address : 2000777c&lt;/p&gt;
&lt;p&gt;allocated address : 200076dc&lt;/p&gt;
&lt;p&gt;allocated address : 2000767c&lt;/p&gt;
&lt;p&gt;allocated address : 2000764c&lt;/p&gt;
&lt;p&gt;allocated address : 2000761c&lt;/p&gt;
&lt;p&gt;allocated address : 2000757c&lt;/p&gt;
&lt;p&gt;allocated address : 2000754c&lt;/p&gt;
&lt;p&gt;allocated address : 200074ec&lt;/p&gt;
&lt;p&gt;allocated address : 2000747c&lt;/p&gt;
&lt;p&gt;allocated address : 2000742c&lt;/p&gt;
&lt;p&gt;allocated address : 200073ac&lt;/p&gt;
&lt;p&gt;allocated address : 2000736c&lt;/p&gt;
&lt;p&gt;allocated address : 2000733c&lt;/p&gt;
&lt;p&gt;allocated address : 2000730c&lt;/p&gt;
&lt;p&gt;allocated address : 200072dc&lt;/p&gt;
&lt;p&gt;allocated address : 200072ac&lt;/p&gt;
&lt;p&gt;allocated address : 2000727c&lt;/p&gt;
&lt;p&gt;allocated address : 2000724c&lt;/p&gt;
&lt;p&gt;allocated address : 2000721c&lt;/p&gt;
&lt;p&gt;allocated address : 2000714c&lt;/p&gt;
&lt;p&gt;allocated address : 2000705c&lt;/p&gt;
&lt;p&gt;allocated address : 20006fbc&lt;/p&gt;
&lt;p&gt;allocated address : 20006f0c&lt;/p&gt;
&lt;p&gt;allocated address : 20006e4c&lt;/p&gt;
&lt;p&gt;allocated address : 20006e1c&lt;/p&gt;
&lt;p&gt;allocated address : 20006d3c&lt;/p&gt;
&lt;p&gt;allocated address : 20006d2c&lt;/p&gt;
&lt;p&gt;allocated address : 20006d1c&lt;/p&gt;
&lt;p&gt;allocated address : 20006d0c&lt;/p&gt;
&lt;p&gt;allocated address : 20006cbc&lt;/p&gt;
&lt;p&gt;allocated address : 20006bfc&lt;/p&gt;
&lt;p&gt;allocated address : 20006b3c&lt;/p&gt;
&lt;p&gt;allocated address : 20006afc&lt;/p&gt;
&lt;p&gt;allocated address : 20006aec&lt;/p&gt;
&lt;p&gt;allocated address : 20006adc&lt;/p&gt;
&lt;p&gt;allocated address : 20006acc&lt;/p&gt;
&lt;p&gt;allocated address : 20006abc&lt;/p&gt;
&lt;p&gt;allocated address : 20006aac&lt;/p&gt;
&lt;p&gt;allocated address : 20006a9c&lt;/p&gt;
&lt;p&gt;allocated address : 20006a8c&lt;/p&gt;
&lt;p&gt;allocated address : 20006a7c&lt;/p&gt;
&lt;p&gt;allocated address : 20006a6c&lt;/p&gt;
&lt;p&gt;allocated address : 20006a5c&lt;/p&gt;
&lt;p&gt;allocated address : 20006a4c&lt;/p&gt;
&lt;p&gt;allocated address : 200069ac&lt;/p&gt;
&lt;p&gt;allocated address : 2000691c&lt;/p&gt;
&lt;p&gt;allocated address : 200068ec&lt;/p&gt;
&lt;p&gt;allocated address : 200068cc&lt;/p&gt;
&lt;p&gt;allocated address : 2000683c&lt;/p&gt;
&lt;p&gt;allocated address : 200067dc&lt;/p&gt;
&lt;p&gt;allocated address : 200067bc&lt;/p&gt;
&lt;p&gt;allocated address : 2000678c&lt;/p&gt;
&lt;p&gt;allocated address : 2000673c&lt;/p&gt;
&lt;p&gt;allocated address : 2000670c&lt;/p&gt;
&lt;p&gt;allocated address : 2000668c&lt;/p&gt;
&lt;p&gt;allocated address : 200065fc&lt;/p&gt;
&lt;p&gt;allocated address : 200065cc&lt;/p&gt;
&lt;p&gt;allocated address : 2000656c&lt;/p&gt;
&lt;p&gt;allocated address : 200064cc&lt;/p&gt;
&lt;p&gt;allocated address : 2000646c&lt;/p&gt;
&lt;p&gt;allocated address : 2000643c&lt;/p&gt;
&lt;p&gt;allocated address : 2000640c&lt;/p&gt;
&lt;p&gt;allocated address : 2000639c&lt;/p&gt;
&lt;p&gt;allocated address : 2000634c&lt;/p&gt;
&lt;p&gt;allocated address : 200062fc&lt;/p&gt;
&lt;p&gt;allocated address : 200062cc&lt;/p&gt;
&lt;p&gt;allocated address : 2000629c&lt;/p&gt;
&lt;p&gt;allocated address : 2000623c&lt;/p&gt;
&lt;p&gt;allocated address : 200061ec&lt;/p&gt;
&lt;p&gt;allocated address : 2000617c&lt;/p&gt;
&lt;p&gt;allocated address : 2000615c&lt;/p&gt;
&lt;p&gt;allocated address : 200060ec&lt;/p&gt;
&lt;p&gt;allocated address : 2000608c&lt;/p&gt;
&lt;p&gt;allocated address : 2000603c&lt;/p&gt;
&lt;p&gt;allocated address : 2000601c&lt;/p&gt;
&lt;p&gt;allocated address : 20005ffc&lt;/p&gt;
&lt;p&gt;allocated address : 20005fdc&lt;/p&gt;
&lt;p&gt;allocated address : 20005fbc&lt;/p&gt;
&lt;p&gt;allocated address : 20005f9c&lt;/p&gt;
&lt;p&gt;allocated address : 20005f6c&lt;/p&gt;
&lt;p&gt;allocated address : 20005f4c&lt;/p&gt;
&lt;p&gt;allocated address : 20005f1c&lt;/p&gt;
&lt;p&gt;allocated address : 20005efc&lt;/p&gt;
&lt;p&gt;allocated address : 20005edc&lt;/p&gt;
&lt;p&gt;allocated address : 20005eac&lt;/p&gt;
&lt;p&gt;allocated address : 20005e8c&lt;/p&gt;
&lt;p&gt;allocated address : 20005e6c&lt;/p&gt;
&lt;p&gt;allocated address : 20005e5c&lt;/p&gt;
&lt;p&gt;allocated address : 20005e3c&lt;/p&gt;
&lt;p&gt;allocated address : 20005ddc&lt;/p&gt;
&lt;p&gt;allocated address : 20005d8c&lt;/p&gt;
&lt;p&gt;allocated address : 20005d2c&lt;/p&gt;
&lt;p&gt;allocated address : 20005d0c&lt;/p&gt;
&lt;p&gt;allocated address : 20005cdc&lt;/p&gt;
&lt;p&gt;allocated address : 20005c3c&lt;/p&gt;
&lt;p&gt;allocated address : 20005c0c&lt;/p&gt;
&lt;p&gt;allocated address : 20005bac&lt;/p&gt;
&lt;p&gt;allocated address : 20005b4c&lt;/p&gt;
&lt;p&gt;allocated address : 20005b1c&lt;/p&gt;
&lt;p&gt;allocated address : 20005aec&lt;/p&gt;
&lt;p&gt;allocated address : 2000596c&lt;/p&gt;
&lt;p&gt;allocated address : 20005a2c&lt;/p&gt;
&lt;p&gt;allocated address : 200059ec&lt;/p&gt;
&lt;p&gt;allocated address : 0&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Finally,&amp;nbsp;for the first and last time allocated address goes up from&amp;nbsp;&lt;span&gt;2000596c to&amp;nbsp;20005a2c and then decreases to 0.&lt;br /&gt;Is it possible to clear the entire heap and start anew, perhaps when the allocated address reaches 0?&lt;/span&gt;&lt;/p&gt;
[quote userid="2115" url="~/f/nordic-q-a/102436/an-issue-with-memory-allocation-in-nrf52832-when-working-with-json-files/439664"]Try to print + when alloc&amp;#39;ing, and - when free&amp;#39;ing memory to see how often one occurs vs. the other.[/quote]
&lt;p&gt;&lt;span&gt;&lt;br /&gt;I&amp;#39;m unsure how to ensure the allocated memory has been freed since the free() function doesn&amp;#39;t return any value to confirm its success. Do you have any suggestions on how to address this issue?&lt;br /&gt;&lt;br /&gt;Thank you for your time.&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: An issue with memory allocation in NRF52832 when working with JSON files</title><link>https://devzone.nordicsemi.com/thread/439664?ContentTypeID=1</link><pubDate>Thu, 03 Aug 2023 10:09:36 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:216e93e5-a9f2-4196-bf0f-39aca5b6b1e6</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Looking at it again I found one place where the memory is free&amp;#39;d:&lt;/p&gt;
[quote user="ZaQa"]allocated address : 20005b5c&lt;br /&gt;allocated address : 20005b1c&lt;br /&gt;allocated address : 20005b8c[/quote]
&lt;p&gt;This is the only place where you actually free the allocated memory, thus the HEAP goes up again. You need to adjust your heap size and/or handle your timing accordingly.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Try to print + when alloc&amp;#39;ing, and - when free&amp;#39;ing memory to see how often one occurs vs. the other.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: An issue with memory allocation in NRF52832 when working with JSON files</title><link>https://devzone.nordicsemi.com/thread/439651?ContentTypeID=1</link><pubDate>Thu, 03 Aug 2023 09:20:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4d52c3b4-a35b-4633-bbce-cc965f1cb1dd</guid><dc:creator>ZaQa</dc:creator><description>&lt;p&gt;Hi&amp;nbsp;&lt;span&gt;H&amp;aring;kon,&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
[quote userid="2115" url="~/f/nordic-q-a/102436/an-issue-with-memory-allocation-in-nrf52832-when-working-with-json-files/439637"]Which compiler are you using? is alloc used in other functions of your firmware?[/quote]
&lt;p&gt;I&amp;#39;m currently using Segger Embedded Studio as IDE and GCC as the compiler.&amp;nbsp;As for the second question, alloc is only utilized within the cJSON code and not elsewhere.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I appreciate your response.&lt;br /&gt;&lt;br /&gt;ZaQa&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: An issue with memory allocation in NRF52832 when working with JSON files</title><link>https://devzone.nordicsemi.com/thread/439637?ContentTypeID=1</link><pubDate>Thu, 03 Aug 2023 08:54:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b3f3e4e8-8fc7-4904-948f-a02001032fc8</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Dynamic memory handling is&amp;nbsp;not always easy to handle, and should ideally only be used when there&amp;#39;s no other option.&lt;/p&gt;
&lt;p&gt;cJSON is one scenario where it makes sense to use dynamic allocation.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
[quote user="ZaQa"]Upon thoroughly examining my code, I have come to the realization that the free() function does not release allocated space. Consequently, over time, the heap section becomes full and ultimately causes the code to fail.[/quote]
&lt;p&gt;Which compiler are you using? is alloc used in other functions of your firmware?&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: An issue with memory allocation in NRF52832 when working with JSON files</title><link>https://devzone.nordicsemi.com/thread/439562?ContentTypeID=1</link><pubDate>Wed, 02 Aug 2023 17:20:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a1a0899a-0948-46cb-a402-d67edc02cf6c</guid><dc:creator>ZaQa</dc:creator><description>[quote userid="2115" url="~/f/nordic-q-a/102436/an-issue-with-memory-allocation-in-nrf52832-when-working-with-json-files/439497"]You must check your return here, if not; you risk losing data.[/quote]
&lt;p&gt;That&amp;#39;s correct. I investigated and now I&amp;#39;m verifying the value returned by the ble_nus_data_send function, which seems to be working properly.&lt;br /&gt;&lt;br /&gt;Upon thoroughly examining my code, I have come to the realization that the free() function does not release allocated space. Consequently, over time, the heap section becomes full and ultimately causes the code to fail.&lt;br /&gt;&lt;br /&gt;Could you please guide me on how to effectively resolve this issue? I would greatly appreciate your kind assistance.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: An issue with memory allocation in NRF52832 when working with JSON files</title><link>https://devzone.nordicsemi.com/thread/439497?ContentTypeID=1</link><pubDate>Wed, 02 Aug 2023 12:26:11 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3d0edfb7-1316-4086-9217-2799334ebf0e</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi ZaQa,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
[quote user="ZaQa"]It seems that multiple&amp;nbsp;function callings&amp;nbsp;and transmitting the mentioned data cause the heap section to fill up after several uses. I have traced some allocated memory addresses in this &lt;span&gt;scenario&lt;/span&gt;:[/quote]
&lt;p&gt;You can lock the function with a semaphore for instance.&lt;/p&gt;
&lt;p&gt;The allocation print indicate that you&amp;#39;re not free&amp;#39;ing the data? It only goes in one direction.&lt;/p&gt;
&lt;p&gt;You have to debug to see where the memory is leaking.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
[quote user=""]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ble_nus_data_send(&amp;amp;m_nus, (uint8_t *)out , &amp;amp;len, m_conn_handle);[/quote]
&lt;p&gt;Sorry, I assumed you were using NCS, but this function call points to nRF5 SDK.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;You must check your return here, if not; you risk losing data.&lt;/p&gt;
&lt;p&gt;In general, I would recommend that you check your returned pointers, also from the cJSON_* calls as well.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: An issue with memory allocation in NRF52832 when working with JSON files</title><link>https://devzone.nordicsemi.com/thread/439415?ContentTypeID=1</link><pubDate>Wed, 02 Aug 2023 07:17:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d63db52d-83bc-4eac-ba53-ccb536a82495</guid><dc:creator>ZaQa</dc:creator><description>&lt;p&gt;Hi,&lt;br /&gt;Thank you for your prompt response.&lt;/p&gt;
[quote userid="2115" url="~/f/nordic-q-a/102436/an-issue-with-memory-allocation-in-nrf52832-when-working-with-json-files/439326"]What is the heap size set to on your end?[/quote]
&lt;p&gt;It&amp;#39;s 8kb (0x20005910 - 0x2000790f).&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;It seems that multiple&amp;nbsp;function callings&amp;nbsp;and transmitting the mentioned data cause the heap section to fill up after several uses. I have traced some allocated memory addresses in this &lt;span&gt;scenario&lt;/span&gt;:&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;allocated address : 200074dc&lt;br /&gt;allocated address : 2000741c&lt;br /&gt;allocated address : 200073dc&lt;br /&gt;allocated address : 200072dc&lt;br /&gt;allocated address : 2000728c&lt;br /&gt;allocated address : 200071cc&lt;br /&gt;allocated address : 2000717c&lt;br /&gt;allocated address : 200070fc&lt;br /&gt;allocated address : 2000709c&lt;br /&gt;allocated address : 2000706c&lt;br /&gt;allocated address : 20006fec&lt;br /&gt;allocated address : 20006fdc&lt;br /&gt;allocated address : 20006f2c&lt;br /&gt;allocated address : 20006e9c&lt;br /&gt;allocated address : 20006e2c&lt;br /&gt;allocated address : 20006dac&lt;br /&gt;allocated address : 20006d3c&lt;br /&gt;allocated address : 20006d2c&lt;br /&gt;allocated address : 20006c4c&lt;br /&gt;allocated address : 20006bdc&lt;br /&gt;allocated address : 20006b6c&lt;br /&gt;allocated address : 20006b3c&lt;br /&gt;allocated address : 20006afc&lt;br /&gt;allocated address : 20006acc&lt;br /&gt;allocated address : 20006a8c&lt;br /&gt;allocated address : 20006a7c&lt;br /&gt;allocated address : 200069cc&lt;br /&gt;allocated address : 200068fc&lt;br /&gt;allocated address : 200068cc&lt;br /&gt;allocated address : 2000681c&lt;br /&gt;allocated address : 2000677c&lt;br /&gt;allocated address : 2000673c&lt;br /&gt;allocated address : 2000669c&lt;br /&gt;allocated address : 2000664c&lt;br /&gt;allocated address : 2000657c&lt;br /&gt;allocated address : 2000655c&lt;br /&gt;allocated address : 200064dc&lt;br /&gt;allocated address : 200063ec&lt;br /&gt;allocated address : 200063cc&lt;br /&gt;allocated address : 2000635c&lt;br /&gt;allocated address : 200062ec&lt;br /&gt;allocated address : 2000626c&lt;br /&gt;allocated address : 2000619c&lt;br /&gt;allocated address : 2000611c&lt;br /&gt;allocated address : 2000609c&lt;br /&gt;allocated address : 2000608c&lt;br /&gt;allocated address : 2000601c&lt;br /&gt;allocated address : 20005fac&lt;br /&gt;allocated address : 20005f3c&lt;br /&gt;allocated address : 20005edc&lt;br /&gt;allocated address : 20005e9c&lt;br /&gt;allocated address : 20005e4c&lt;br /&gt;allocated address : 20005ddc&lt;br /&gt;allocated address : 20005d7c&lt;br /&gt;allocated address : 20005d1c&lt;br /&gt;allocated address : 20005cec&lt;br /&gt;allocated address : 20005c5c&lt;br /&gt;allocated address : 20005bec&lt;br /&gt;allocated address : 20005b5c&lt;br /&gt;allocated address : 20005b1c&lt;br /&gt;allocated address : 20005b8c&lt;br /&gt;allocated address : 20005b2c&lt;br /&gt;allocated address : 20005a4c&lt;/p&gt;
&lt;p&gt;As you may have noticed, the beginning and end of these addresses are quite close to the start and end of the heap section. Is there anything I can do to prevent this? My goal is for users to be able to access this data as frequently as they desire through the mobile app, but it currently only works for a limited time.&lt;br /&gt;&lt;br /&gt;I appreciate the time you have taken.&lt;br /&gt;&lt;br /&gt;ZaQa&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: An issue with memory allocation in NRF52832 when working with JSON files</title><link>https://devzone.nordicsemi.com/thread/439326?ContentTypeID=1</link><pubDate>Tue, 01 Aug 2023 15:21:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e3cd35c3-0a71-4662-a10c-f90e5254fc59</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
[quote user=""]&lt;p&gt;I have read in a thread that applying cJSON_Init() to the main would be helpful, but when I added this function, the JSON parse data process failed:&lt;/p&gt;
&lt;p&gt;cJSON_Parse(received_data)&amp;nbsp;--&amp;gt; cJSON_ParseWithOpts&amp;nbsp;--&amp;gt; cJSON_New_Item&amp;nbsp;--&amp;gt; cJSON_malloc&lt;/p&gt;
&lt;p&gt;I suspect that there is something wrong with memory allocation. Could you please help me with this problem? Is there any configuration or consideration that I should have taken but missed out on?&lt;/p&gt;[/quote]
&lt;p&gt;What is the heap size set to on your end?&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;Please note that you&amp;#39;re using libc dynamic alloc functions, ie. &amp;quot;malloc()&amp;quot; instead of zephyr&amp;#39;s kernel heap &amp;quot;k_malloc()&amp;quot;- this will use the heap by your selected libc (normally newlib or minimal libc, symbols NEWLIB_LIBC_MIN_REQUIRED_HEAP_SIZE /&amp;nbsp;MINIMAL_LIBC_MALLOC_ARENA_SIZE)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>