Heap memory depleting

hadif66
Posts: 2
Joined: Sun Sep 11, 2022 9:03 am

Heap memory depleting

Postby hadif66 » Tue Jan 24, 2023 4:52 am

Hi, I am using nvs flash to store variables in my code and when I try to load the variables into pointers from flash memory, each time it fills some portion of heap memory and won't release it, I will post part of the code that causes this problem, please let me know what makes this issue:

nvs_manager.c file:

Code: Select all

esp_err_t get_nvs_int(char* name, int **param)
{
    nvs_handle_t nvs;

    esp_err_t err = nvs_open(PARAM_NAMESPACE, NVS_READONLY, &nvs);
    if (err == ESP_OK) {
        if ( (err = nvs_get_i32(nvs, name, (int32_t*)(param))) == ESP_OK) {
            //ESP_LOGI(TAG, "%s %d", name, *param);
        } else {
            return err;
        }
        nvs_close(nvs);
    } else {
        return err;
    }
    return ESP_OK;
}
and here is the part of code in the main class:

Code: Select all

unsigned long myRF1,myRF2,myRF3,myRF4,myRF5,myRF6,myRF7,myRF8,myRF9,myRF10,myRF11,myRF12,myRF13,myRF14,myRF15;
static void receiver(void* arg)
{
	ESP_LOGI(TAG, "Start receiver");
	RCSWITCH_t RCSwitch;
	initSwich(&RCSwitch);
	//enableReceive(&RCSwitch, 22);
	enableReceive(&RCSwitch, CONFIG_RF_GPIO);
    int statCounter = 0;
	while (true) {
        RCValue = NULL;
		if (available(&RCSwitch)) {
			ESP_LOGI(TAG, "Received %lu / %dbit Protocol: %d", 
            getReceivedValue(&RCSwitch), getReceivedBitlength(&RCSwitch), getReceivedProtocol(&RCSwitch));

            RCValue = getReceivedValue(&RCSwitch);

            printf("binary=%s\n",long_to_binary(RCValue));
            //printf("First 28=%s\n",long_to_binary_first_28_bits(RCValue));
            
            unsigned long masked = (RCValue & mask_key28);
            //printf("Last 4=%ld\n",masked);

            //get_nvs_str
            myRF1 = 0;
            myRF2 = 0;
            myRF3 = 0;
            myRF4 = 0;
            myRF5 = 0;
            myRF6 = 0;
            myRF7 = 0;
            myRF8 = 0;
            myRF9 = 0;
            myRF10 = 0;
            myRF11 = 0;
            myRF12 = 0;
            myRF13 = 0;
            myRF14 = 0;
            myRF15 = 0;

            int tempId = 1;
            if(masked != 0000L){

                get_nvs_int("remote1",&myRF1);
                get_nvs_int("remote2",&myRF2);
                get_nvs_int("remote3",&myRF3);
                get_nvs_int("remote4",&myRF4);
                get_nvs_int("remote5",&myRF5);
                get_nvs_int("remote6",&myRF6);
                get_nvs_int("remote7",&myRF7);
                get_nvs_int("remote8",&myRF8);
                get_nvs_int("remote9",&myRF9);
                get_nvs_int("remote10",&myRF10);
                get_nvs_int("remote11",&myRF11);
                get_nvs_int("remote12",&myRF12);
                get_nvs_int("remote13",&myRF13);
                get_nvs_int("remote14",&myRF14);
                get_nvs_int("remote15",&myRF15);


                if((myRF1 & mask28)  == (RCValue & mask28)
                || (myRF2 & mask28)  == (RCValue & mask28) 
                || (myRF3 & mask28)  == (RCValue & mask28) 
                || (myRF4 & mask28)  == (RCValue & mask28) 
                || (myRF5 & mask28)  == (RCValue & mask28) 
                || (myRF6 & mask28)  == (RCValue & mask28) 
                || (myRF7 & mask28)  == (RCValue & mask28) 
                || (myRF8 & mask28)  == (RCValue & mask28) 
                || (myRF9 & mask28)  == (RCValue & mask28) 
                || (myRF10 & mask28)  == (RCValue & mask28) 
                || (myRF11 & mask28)  == (RCValue & mask28) 
                || (myRF12 & mask28)  == (RCValue & mask28) 
                || (myRF13 & mask28)  == (RCValue & mask28) 
                || (myRF14 & mask28)  == (RCValue & mask28) 
                || (myRF15 & mask28)  == (RCValue & mask28)){
                    ESP_LOGE(TAG,"Remote Matched");
                    switch (masked){
                        case CONFIG_KEY_1:
                          //........
                            break;
                        case CONFIG_KEY_2:  
                           //........
                            break;                                             
                        default:
                            printf("Key is not defined \n");
                            break;
                    }
                }else{
                    ESP_LOGI(TAG,"Remote Controller didn't match");
                }
                    
            }


			resetAvailable(&RCSwitch);
		} 
        vTaskDelay(300/portTICK_PERIOD_MS);
	} // end while
    vTaskDelete(NULL);
}

void app_main(void)
{    //Initialize NVS
    esp_err_t ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
      ESP_ERROR_CHECK(nvs_flash_erase());
      ret = nvs_flash_init();
    }
    
    xTaskCreate(receiver, "RECV", 1024*4, NULL, 0, NULL);
    
}
I have same issue when trying to load or save variables into nvs flash in my other tasks.

Who is online

Users browsing this forum: No registered users and 61 guests