Giter Club home page Giter Club logo

Comments (2)

elgerg avatar elgerg commented on July 28, 2024

Ok,

Having using valgrind to debug the app, it always look like it's in the json_object_put call that is trying to free memory that has been freed.

This to me seems a little odd given the order of add/removes IE:

void handleForecast(json_object *parsed_json)
{
	struct json_object *weatherObj, *tmpWeather;
    int exists = json_object_object_get_ex(parsed_json, "payload", &weatherObj);
    if ( false == exists )
    {
        printf( "\"payload\" not found in JSON\n" );
        return;
    }

    lv_mem_monitor_t mon;
    lv_mem_monitor(&mon);
    for (int i = 0; i < json_object_array_length( weatherObj ) && i < 20; i++ )
    {
		tmpWeather = json_object_array_get_idx( weatherObj, i );

		struct json_object *dt_txt,
							*feels_like,
							*temp_min,
							*temp_max,
							*wmain,
							*description,
							*rain,
							*icon,
							*wind;
		json_object_object_get_ex(tmpWeather, "dt_txt", &dt_txt);
		json_object_object_get_ex(tmpWeather, "feels_like_raw", &feels_like);
		json_object_object_get_ex(tmpWeather, "temp_min_raw", &temp_min);
		json_object_object_get_ex(tmpWeather, "temp_max_raw", &temp_max);
		json_object_object_get_ex(tmpWeather, "main", &wmain);
		json_object_object_get_ex(tmpWeather, "description", &description);
		json_object_object_get_ex(tmpWeather, "rain", &rain);
		json_object_object_get_ex(tmpWeather, "wind", &wind);
		json_object_object_get_ex(tmpWeather, "icon", &icon);
		printf("Dt::::: feel: %s Cal: %s Min: %s Max: %s  Main: %s Description: %s Rain: %s Wind: %s Icon: %s\n", json_object_get_string(dt_txt)
											, json_object_get_string(feels_like)
											, json_object_get_string(temp_min)
											, json_object_get_string(temp_max)
											, json_object_get_string(wmain)
											, json_object_get_string(description)
											, json_object_get_string(rain)
											, json_object_get_string(wind)
											, json_object_get_string(icon)
											);

		lv_label_set_text(temps.weatherItems[i].dt, json_object_get_string(dt_txt));

		//lv_obj_del(temps.weatherItems[i].icon);
		//temps.weatherItems[i].icon = lv_rlottie_create_from_file(weatherPage, LOTTIE_IMAGE_SIZE, LOTTIE_IMAGE_SIZE, resolve_owm_icon_lottie( json_object_get_string(icon) ));

		int ramCurrent = (int)mon.total_size - mon.free_size;
		printDateTime();
		printf("Total Size: %d, Free Size: %d, Current %d\n", mon.total_size, mon.free_size, ramCurrent/10);

	    //lv_obj_align_to(temps.weatherItems[i].icon, temps.weatherItems[i].dt, LV_ALIGN_BOTTOM_MID, 10, 90);
		lv_img_set_src(temps.weatherItems[i].icon, (lv_obj_t*)resolve_owm_icon( json_object_get_string(icon) ));

		char strColour[7];
		getColourForTemp(strColour, json_object_get_double(feels_like));
        lv_label_set_text_fmt(temps.weatherItems[i].feels, "Feels: #%s %0.2f°c#", strColour, json_object_get_double(feels_like));

		//lv_label_set_text(temps.weatherItems[i].feels, json_object_get_string(feels_like));

		getColourForTemp(strColour, json_object_get_double(temp_min));
        lv_label_set_text_fmt(temps.weatherItems[i].min, "Min: #%s %0.2f°c#", strColour, json_object_get_double(temp_min));

		//lv_label_set_text(temps.weatherItems[i].min, json_object_get_string(temp_min));

		getColourForTemp(strColour, json_object_get_double(temp_max));
        lv_label_set_text_fmt(temps.weatherItems[i].max, "Max: #%s %0.2f°c#", strColour, json_object_get_double(temp_max));

		//lv_label_set_text(temps.weatherItems[i].max, json_object_get_string(temp_max));

		lv_label_set_text(temps.weatherItems[i].rain, json_object_get_string(rain));

		lv_label_set_text(temps.weatherItems[i].wind, json_object_get_string(wind));

		lv_label_set_text(temps.weatherItems[i].bmain, json_object_get_string(wmain));

		lv_label_set_text(temps.weatherItems[i].desc, json_object_get_string(description));

		json_object_put(dt_txt);
		json_object_put(feels_like);
		json_object_put(temp_min);
		json_object_put(temp_max);
		json_object_put(rain);
		json_object_put(wind);
		json_object_put(wmain);
		json_object_put(description);
		json_object_put(icon);

		//json_object_put(tmpWeather);
	}
	//json_object_put(weatherObj);

}

Here is what valgrand says when the json_object_put(tmpWeather); is uncommented:

Invalid read of size 4
==136976== by 0x27BDA3: handleForecast (Callbacks.c:104)
IE: json_object_put(tmpWeather);

==136976== Address 0x60d93a4 is 4 bytes inside a block of size 65 free'd
==136976== by 0x27BD5B: handleForecast (Callbacks.c:94)
IE: json_object_put(dt_txt);

==136976== Invalid read of size 4
==136976==    at 0x48C5CCC: json_object_put (in /usr/lib/aarch64-linux-gnu/libjson-c.so.5.2.0)
==136976==    by 0x48CB7BF: lh_table_free (in /usr/lib/aarch64-linux-gnu/libjson-c.so.5.2.0)
==136976==    by 0x48C5D5F: json_object_put (in /usr/lib/aarch64-linux-gnu/libjson-c.so.5.2.0)
==136976==    by 0x27BDA3: handleForecast (Callbacks.c:104)
==136976==    by 0x27E3F7: my_message_callback (MQTT.c:54)
==136976==    by 0x48F7603: ??? (in /usr/lib/aarch64-linux-gnu/libmosquitto.so.2.0.11)
==136976==    by 0x48FD4E7: ??? (in /usr/lib/aarch64-linux-gnu/libmosquitto.so.2.0.11)
==136976==    by 0x48F8703: mosquitto_loop_read (in /usr/lib/aarch64-linux-gnu/libmosquitto.so.2.0.11)
==136976==    by 0x48F8B37: mosquitto_loop (in /usr/lib/aarch64-linux-gnu/libmosquitto.so.2.0.11)
==136976==    by 0x28B70F: tsk_runmosq (Tasks.c:15)
==136976==    by 0x2566FB: lv_timer_exec (lv_timer.c:313)
==136976==    by 0x25619F: lv_timer_handler (lv_timer.c:109)
==136976==  Address 0x60d93a4 is 4 bytes inside a block of size 65 free'd
==136976==    at 0x4887B40: free (in /usr/libexec/valgrind/vgpreload_memcheck-arm64-linux.so)
==136976==    by 0x48C5D23: json_object_put (in /usr/lib/aarch64-linux-gnu/libjson-c.so.5.2.0)
==136976==    by 0x27BD5B: handleForecast (Callbacks.c:94)
==136976==    by 0x27E3F7: my_message_callback (MQTT.c:54)
==136976==    by 0x48F7603: ??? (in /usr/lib/aarch64-linux-gnu/libmosquitto.so.2.0.11)
==136976==    by 0x48FD4E7: ??? (in /usr/lib/aarch64-linux-gnu/libmosquitto.so.2.0.11)
==136976==    by 0x48F8703: mosquitto_loop_read (in /usr/lib/aarch64-linux-gnu/libmosquitto.so.2.0.11)
==136976==    by 0x48F8B37: mosquitto_loop (in /usr/lib/aarch64-linux-gnu/libmosquitto.so.2.0.11)
==136976==    by 0x28B70F: tsk_runmosq (Tasks.c:15)
==136976==    by 0x2566FB: lv_timer_exec (lv_timer.c:313)
==136976==    by 0x25619F: lv_timer_handler (lv_timer.c:109)
==136976==    by 0x1E6E1F: lv_task_handler (lv_api_map.h:37)
==136976==  Block was alloc'd at
==136976==    at 0x48850C8: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-arm64-linux.so)
==136976==    by 0x48C541B: ??? (in /usr/lib/aarch64-linux-gnu/libjson-c.so.5.2.0)
==136976==    by 0x48C9E03: json_tokener_parse_ex (in /usr/lib/aarch64-linux-gnu/libjson-c.so.5.2.0)
==136976==    by 0x48CA4DF: json_tokener_parse_verbose (in /usr/lib/aarch64-linux-gnu/libjson-c.so.5.2.0)
==136976==    by 0x48CA553: json_tokener_parse (in /usr/lib/aarch64-linux-gnu/libjson-c.so.5.2.0)
==136976==    by 0x27E3AF: my_message_callback (MQTT.c:47)
==136976==    by 0x48F7603: ??? (in /usr/lib/aarch64-linux-gnu/libmosquitto.so.2.0.11)
==136976==    by 0x48FD4E7: ??? (in /usr/lib/aarch64-linux-gnu/libmosquitto.so.2.0.11)
==136976==    by 0x48F8703: mosquitto_loop_read (in /usr/lib/aarch64-linux-gnu/libmosquitto.so.2.0.11)
==136976==    by 0x48F8B37: mosquitto_loop (in /usr/lib/aarch64-linux-gnu/libmosquitto.so.2.0.11)
==136976==    by 0x28B70F: tsk_runmosq (Tasks.c:15)
==136976==    by 0x2566FB: lv_timer_exec (lv_timer.c:313)
==136976== 

from json-c.

hawicz avatar hawicz commented on July 28, 2024

This is not a bug in json-c.
json_object_object_get_ex doesn't give you ownership of the object returned, it's still owned by the containing object so you must not call json_object_put on those returned objects.
All you need to do to free all memory used by a object tree is call json_object_put on the top-level object, presumably parsed_json in your example. (unless you've explicitly incremented reference counts somewhere with json_object_get)

from json-c.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.