<?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>cJSON_PrintUnformatted  err = -ENOMEM;</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/112086/cjson_printunformatted-err--enomem</link><description>Hi, 
 I am using CJSON_LIB from nrf-sdk modules, I even tested this cjson lib in such a simple way in helloworld app (I attached the sample project.), and after running for a while, I have a no memory space issue, Any idea? I was expecting to free up</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 19 Jun 2024 07:54:15 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/112086/cjson_printunformatted-err--enomem" /><item><title>RE: cJSON_PrintUnformatted  err = -ENOMEM;</title><link>https://devzone.nordicsemi.com/thread/489419?ContentTypeID=1</link><pubDate>Wed, 19 Jun 2024 07:54:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e02bbc85-e044-47fa-8b7a-c3d8e70ef0ca</guid><dc:creator>Marte Myrvold</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;The Asset Tracker v2 uses the QoS module to retransmit payloads (the allocated JSON strings) until they have been acked. Once that happens, it is freed here: &lt;a href="https://github.com/nrfconnect/sdk-nrf/blob/v2.6.1/applications/asset_tracker_v2/src/modules/cloud_module.c#L812"&gt;https://github.com/nrfconnect/sdk-nrf/blob/v2.6.1/applications/asset_tracker_v2/src/modules/cloud_module.c#L812&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;br /&gt;Marte&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: cJSON_PrintUnformatted  err = -ENOMEM;</title><link>https://devzone.nordicsemi.com/thread/488661?ContentTypeID=1</link><pubDate>Thu, 13 Jun 2024 11:46:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:81037448-da05-40ed-867b-f1ede74581ba</guid><dc:creator>zsn</dc:creator><description>&lt;p&gt;hi &lt;a href="https://devzone.nordicsemi.com/members/marte.m"&gt;Marte Myrvold&lt;/a&gt;&amp;nbsp; thanks for your feedback. Ill correct all those issues. FYI, It seems nrf asset tracker app also has the same problem. I tried to explain here above in my previous comment:&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
[quote userid="117272" url="~/f/nordic-q-a/112086/cjson_printunformatted-err--enomem/488652"]&lt;p&gt;but it was surprising to me, I took nrf applications as a reference to create my own sample&amp;nbsp;and I didnt see anywhere nrf developers call&amp;nbsp;&lt;span style="font-family:arial, helvetica, sans-serif;"&gt;cJSON_free functions. Except here&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class="evolution-code-editor theme-clouds"&gt;&lt;a href="#" class="fullscreen"&gt;Fullscreen&lt;/a&gt;&lt;div style="width:100%;height:42px;" class=" ace_editor ace-clouds"&gt;&lt;div class="ace_gutter"&gt;&lt;div class="ace_layer ace_gutter-layer ace_folding-enabled" style="margin-top:0px;height:70px;width:40px;"&gt;&lt;div class="ace_gutter-cell " style="height:14px;"&gt;1&lt;/div&gt;&lt;div class="ace_gutter-cell " style="height:14px;"&gt;2&lt;/div&gt;&lt;div class="ace_gutter-cell " style="height:14px;"&gt;3&lt;/div&gt;&lt;/div&gt;&lt;div class="ace_gutter-active-line" style="display:none;"&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="ace_scroller" style="left:40px;right:0px;bottom:0px;"&gt;&lt;div class="ace_content" style="margin-top:0px;width:694px;height:70px;margin-left:0px;"&gt;&lt;div class="ace_layer ace_print-margin-layer"&gt;&lt;div class="ace_print-margin" style="left:531.825px;"&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="ace_layer ace_marker-layer"&gt;&lt;/div&gt;&lt;div class="ace_layer ace_text-layer" style="padding:0px 4px;"&gt;&lt;div class="ace_line" style="height:14px;"&gt;if (IS_ENABLED(CONFIG_CLOUD_CODEC_LOG_LEVEL_DBG)) {&lt;/div&gt;&lt;div class="ace_line" style="height:14px;"&gt; json_print_obj(&amp;quot;Encoded message:\n&amp;quot;, root_obj);&lt;/div&gt;&lt;div class="ace_line" style="height:14px;"&gt; }&lt;/div&gt;&lt;/div&gt;&lt;div class="ace_layer ace_marker-layer"&gt;&lt;/div&gt;&lt;div class="ace_layer ace_cursor-layer ace_hidden-cursors"&gt;&lt;div class="ace_cursor" style="left:4px;top:0px;width:6.59781px;height:14px;"&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="ace_scrollbar ace_scrollbar-v" style="display:none;width:22px;bottom:0px;"&gt;&lt;div class="ace_scrollbar-inner" style="width:22px;height:42px;"&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="ace_scrollbar ace_scrollbar-h" style="display:none;height:22px;left:40px;right:0px;"&gt;&lt;div class="ace_scrollbar-inner" style="height:22px;width:694px;"&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="height:auto;width:auto;top:0px;left:0px;position:absolute;white-space:pre;font:inherit;overflow:hidden;"&gt;&lt;div style="height:auto;width:auto;top:0px;left:0px;position:absolute;white-space:pre;font:inherit;overflow:visible;"&gt;&lt;/div&gt;&lt;div style="height:auto;width:auto;top:0px;left:0px;position:absolute;white-space:pre;font-style:inherit;font-size:inherit;line-height:inherit;font-family:inherit;overflow:visible;"&gt;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="content-scrollable-wrapper" style="max-width:100%;overflow:auto;max-height:none;"&gt;&lt;pre class="ui-code" data-mode="text" style="display:none;"&gt;if (IS_ENABLED(CONFIG_CLOUD_CODEC_LOG_LEVEL_DBG)) {
 json_print_obj(&amp;quot;Encoded message:\n&amp;quot;, root_obj);
 }&lt;/pre&gt;&lt;/div&gt;&lt;span class="pl-en"&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:arial, helvetica, sans-serif;"&gt;But this is not enough, if a user doesnt enable the &lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:arial, helvetica, sans-serif;"&gt;CONFIG_CLOUD_CODEC_LOG_LEVEL_DBG, &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial, helvetica, sans-serif;"&gt;the application doesnt free the memory. So seems this is a problem,&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:arial, helvetica, sans-serif;"&gt;Am I right?&lt;/span&gt;&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/nrfconnect/sdk-nrf/blob/main/applications/asset_tracker_v2/src/cloud/cloud_codec/aws_iot/aws_iot_codec.c#L371"&gt;https://github.com/nrfconnect/sdk-nrf/blob/main/applications/asset_tracker_v2/src/cloud/cloud_codec/aws_iot/aws_iot_codec.c#L371&lt;/a&gt;&lt;/p&gt;[/quote]
&lt;p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: cJSON_PrintUnformatted  err = -ENOMEM;</title><link>https://devzone.nordicsemi.com/thread/488658?ContentTypeID=1</link><pubDate>Thu, 13 Jun 2024 11:40:25 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:81a96221-b61e-4f56-a7d5-40b342b0219a</guid><dc:creator>Marte Myrvold</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;It seems like you have some memory leaks in your application.&lt;/p&gt;
&lt;p&gt;First of all, buffers allocated by cJSON_PrintUnformatted() must be freed. In your code, it is not freed before the usb_log structure is re-initialized, which means that every time encode_json() is called, a new buffer is allocated, and the old one is lost without being freed. You can free it with cJSON_free():&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;char *buffer = cJSON_PrintUnformatted(root_obj);

...

exit:
	cJSON_Delete(root_obj);
    cJSON_free(json_str);
	printk(&amp;quot;%d %s\n&amp;quot;, __LINE__, FILE_NAME);

return;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Another issue is that if any of the error checks fail before sub_obj and data_obj are added as child objects to root_obj, then sub_obj and data_obj will not be deleted when root_obj is deleted. So you should also make sure that these objects are deleted in the exit as well.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;br /&gt;Marte&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: cJSON_PrintUnformatted  err = -ENOMEM;</title><link>https://devzone.nordicsemi.com/thread/488652?ContentTypeID=1</link><pubDate>Thu, 13 Jun 2024 11:09:21 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e3b20c9d-8aa6-4ebf-9838-5f99c1791f8e</guid><dc:creator>zsn</dc:creator><description>&lt;p&gt;it seems I fixed the issue by following this ticket:&amp;nbsp;&lt;a id="" href="https://github.com/DaveGamble/cJSON/issues/297#issuecomment-751432005"&gt;https://github.com/DaveGamble/cJSON/issues/297&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;this is the fix:&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;pre class="notranslate"&gt;&lt;span class="pl-k"&gt;&lt;/span&gt;&lt;pre class="ui-code" data-mode="text"&gt;const char * jsonString = cJSON_PrintUnformatted(root);
//...
cJSON_free(jsonString);
cJSON_Delete(root);&lt;/pre&gt;&lt;/pre&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;but it was surprising to me, I took nrf applications as a reference to create my own sample&amp;nbsp;and I didnt see anywhere nrf developers call&amp;nbsp;&lt;span style="font-family:arial, helvetica, sans-serif;"&gt;cJSON_free functions. Except here&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;if (IS_ENABLED(CONFIG_CLOUD_CODEC_LOG_LEVEL_DBG)) {
 json_print_obj(&amp;quot;Encoded message:\n&amp;quot;, root_obj);
 }&lt;/pre&gt;&lt;span class="pl-en"&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:arial, helvetica, sans-serif;"&gt;But this is not enough, if a user doesnt enable the &lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:arial, helvetica, sans-serif;"&gt;CONFIG_CLOUD_CODEC_LOG_LEVEL_DBG, &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial, helvetica, sans-serif;"&gt;the application doesnt free the memory. So seems this is a problem,&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:arial, helvetica, sans-serif;"&gt;Am I right?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/nrfconnect/sdk-nrf/blob/main/applications/asset_tracker_v2/src/cloud/cloud_codec/aws_iot/aws_iot_codec.c#L371"&gt;https://github.com/nrfconnect/sdk-nrf/blob/main/applications/asset_tracker_v2/src/cloud/cloud_codec/aws_iot/aws_iot_codec.c#L371&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>