Rest Server

Pedrojdi
Posts: 9
Joined: Mon Mar 08, 2021 2:14 pm

Rest Server

Postby Pedrojdi » Sun Apr 11, 2021 8:10 pm

Colleagues programmers would like information, I set up through esp32 a rest application according to the example of Espressif. Everything is working but it has a detail that I couldn't solve about a variable.

this is part of the espressif example

Code: Select all

/* Simple handler for getting system handler */
static esp_err_t system_info_get_handler(httpd_req_t *req)
{
    httpd_resp_set_type(req, "application/json");
    cJSON *root = cJSON_CreateObject();
    esp_chip_info_t chip_info;
    esp_chip_info(&chip_info);
    cJSON_AddStringToObject(root, "version", IDF_VER);
    cJSON_AddNumberToObject(root, "cores", chip_info.cores);
    const char *sys_info = cJSON_Print(root);
    httpd_resp_sendstr(req, sys_info);
    free((void *)sys_info);
    cJSON_Delete(root);
    return ESP_OK;
}
What I did I replaced
const char * sys_info
for
std :: string sys_info
and removed
free((void *)sys_info);

Code: Select all

static esp_err_t system_info_get_handler(httpd_req_t *req)
{
    httpd_resp_set_type(req, "application/json");
    cJSON *root = cJSON_CreateObject();
    esp_chip_info_t chip_info;
    esp_chip_info(&chip_info);
    cJSON_AddStringToObject(root, "version", IDF_VER);
    cJSON_AddNumberToObject(root, "cores", chip_info.cores);

    			//const char *sys_info = cJSON_Print(root); (replace)
			std::string sys_info = cJSON_Print(root);

    httpd_resp_sendstr(req, sys_info);

   			//free((void *)sys_info); (removed)

    cJSON_Delete(root);
    return ESP_OK;
}
this is working but the only problem is that whenever I make a request like for example in the postman I keep monitoring the memory and with each request the impression it creates a new variable std :: string sys_info because the available memory drops, work as in the first example with free ((void *) sys_info); the available memory does not change. Can someone give me guidance

ESP_Sprite
Posts: 9764
Joined: Thu Nov 26, 2015 4:08 am

Re: Rest Server

Postby ESP_Sprite » Mon Apr 12, 2021 3:27 am

This is because cJSON_Print returns a buffer that needs to be freed by the caller. The original code did this with the free() call. You use this buffer to generate a (separate) std::string object and don't free the string, hence the memory leak.

Pedrojdi
Posts: 9
Joined: Mon Mar 08, 2021 2:14 pm

Re: Rest Server

Postby Pedrojdi » Mon Apr 12, 2021 3:44 pm

how to release the variable of type std :: string so that there is no memory leak or it is not possible

Pedrojdi
Posts: 9
Joined: Mon Mar 08, 2021 2:14 pm

Re: Rest Server

Postby Pedrojdi » Mon Apr 12, 2021 6:39 pm

I posted this example for clarity, but any std :: string variable that I declare an example:

std :: string teste = "teste";

every time "system_info_get_handler" is called, the memory decreases and if I take out the variables of type string it always remains stable
gives the impression that the test variable is not removed from memory and a new one is always created for each call

Who is online

Users browsing this forum: abbas1375 and 103 guests