System does not free memory

I am programming for the Arduino to receive data via Bluetooth by HC-06, the information is passed in the format equal to the get requests (parameter=value&parameter=value) and the system breaks the information by ‘&’ and then by ‘=’ to later verify what are the parameters and what are the parameter values, however, every time the system receives the information, the amount of memory only decreases instead of releasing the memory that was used to process the information and I am not able to identify the problem of not lirerar the memory.

Codes:

Main file:

#include <SoftwareSerial.h>
#include <LinkedList.h>

#define RxB 2
#define TxB 3

SoftwareSerial HC06(RxB,TxB);

void setup() {
  Serial.begin(9600);
  HC06.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  if(HC06.available()) {
    getDataBluetooth();
  }
}

File with code to process the HC-06 data:

struct Node {
  char* param;
  char* value;
  Node* next;
};  

struct Node *head;

void getDataBluetooth() {
  if(HC06.available()) {
    char* vectorAction;
    
    freeRam(); 

    // reads the information that hc received
    vectorAction = readBluetooth();
    
    // extract string information
    setValuesParamsList(vectorAction);

    char* getValue;
    
    // print params and values
    displayList();
	
    // delete content Node
    deleteLinkedList();
	
    freeRam();
    
  }
  
}

void  freeRam() {
  extern int __heap_start,*__brkval;
  int v;
  int value;
  value = (int)&v - (__brkval == 0 ? (int)&__heap_start : (int) __brkval);  

  HC06.print(F("SRAM = "));
  HC06.println(value);
}

void displayList() {
  Node *list = head;
  while(list) {
    HC06.print(list->param);
    HC06.print(F(" -  "));
    HC06.println(list->value);    
    list = list->next;
  }
}

char* readBluetooth() {
  char* vectorAction;

  delay(200);

  LinkedList<char> myLinkedList = LinkedList<char>();  
  char c;
  while(HC06.available()>0) {      
    c = HC06.read();
    myLinkedList.add(c);
  }   
  
  vectorAction = new char[myLinkedList.size()+1];
  
  for(int i=0; i < myLinkedList.size(); i++) {
    vectorAction[i] = myLinkedList.get(i);
  }
  
  vectorAction[myLinkedList.size()] = '\0';
  
  return vectorAction;
}

void setValuesParamsList(char* bluetoothString) {
  
  char* pch;
  char** paramsValues;
  int line = 0;
  
  pch = strtok(bluetoothString,"#");

  paramsValues = new char*[sizeof(pch)];
  
  while (pch != NULL) {
    if(strstr(pch, "=")) {      
      paramsValues[line] = new char[strlen(pch)+1];
      strcpy(paramsValues[line], pch);            
      line++;
    }
    pch = strtok (NULL, "#");
  }

  char* param; 
  char* value;
  int cont = 0;

  // separa o parametro e o valor para inserir na lista
  pch = NULL;  
  for(int i = 0; i < sizeof(paramsValues); i++) {
    pch = strtok (paramsValues[i],"=");
    //Serial.println(pch);
    while (pch != NULL) {
      if(cont == 0) {
        param = new char[sizeof(pch)+1];
        param = pch;     
      } else {
        value = new char[sizeof(pch)+1];
        value = pch;
      }

      cont++;
      pch = strtok (NULL, "=");
    }
    
    mainNode(param, value);
    cont = 0;  
  }

}

char* getParamValue(char* param) {
  Node* list = head;
  
  while(list) {
    if(strcmp(list->param, param) == 0) {
        return list->value;
    }
    list = list->next;
  }
  return NULL;
}

void mainNode(char* param, char* value) {
  
  if(countItensList()==0) {
    initNode(param, value);
  } else {
    addNode(param, value);
  }
  
}

int countItensList() {
  int count = 0;
  Node *list = head;
  
  while(list) {
    count++;
        
    list = list->next;
  }
  return count;
} 

void initNode(char* n, char* n2){  
  head = new Node;
  head->param = n;
  head->value = n2;  
  head->next = NULL;
}

void addNode(char* n, char* n2) {
  Node *newNode = new Node;
  
  newNode->param = n;
  newNode->value = n2;
  
  newNode->next = NULL;

  while(head) {
    if(head->next == NULL) {
      head->next = newNode;
      return;
    } 
    head = head->next;
  }
}

void deleteLinkedList() {
  struct Node *tmpNode;
  while(head) {
    tmpNode = head;
    head = tmpNode->next;
    delete tmpNode;
  }
}

In my understanding, whenever a function returns, the memory that was allocated to it is released after it is used, in which case, after occupying memory to store the processing data the amount of final memory should be equal to the quantity at the beginning of execution … or not?

vectorAction = new char[myLinkedList.size()+1];

vectorAction is a local pointer to char. It is indeed going away when the function ends.

But the char array that you are creating with new for vectorAction to point to, that lives on the heap. And unless you free it, it just stays there. So whenever you are done with that pointer, you need to call free on it to release that memory that it points to.

Your "algorithm" is over-complicated and to be frankly so bad. You are just reading a string of chars and parse it immediately. Why the need for a linkedlist?

Also you are abusing the heap so much that if it could cry it would.

Look here: simple way to receive data

Delta_G: vectorAction = new char[myLinkedList.size()+1];

vectorAction is a local pointer to char. It is indeed going away when the function ends.

But the char array that you are creating with new for vectorAction to point to, that lives on the heap. And unless you free it, it just stays there. So whenever you are done with that pointer, you need to call free on it to release that memory that it points to.

Generally when it is a variable of type "char * varr = new (...)" I release using the command "delete [] varr;" however, this is not working, probably because I declare it as char ** to be able to create a dynamic array.

I tried delete[] variable, delete(variable), free(variable) ..., in these tests nothing happened or failed with the content string of my Node.

How can I release in the case of a char** ?

arduino_new: Your "algorithm" is over-complicated and to be frankly so bad. You are just reading a string of chars and parse it immediately. Why the need for a linkedlist?

Also you are abusing the heap so much that if it could cry it would.

Look here: simple way to receive data

About the code, it was a lot of work because this code was not for atmega328 but for an attiny85, and because attiny was very small I wrote the code in that way (to read data from a string in the format get: parameter=value¶meter=value), but I had to give up the attiny and go back to the atmega!

andresilva: About the code, it was a lot of work because this code was not for atmega328 but for an attiny85, and because attiny was very small I wrote the code in that way (to read data from a string in the format get: parameter=value¶meter=value), but I had to give up the attiny and go back to the atmega!

I don't see a reason (at least in the code posted) why you need to use linkedlist and dynamic memory at all.

If you want to parse a command and execute it sequencially, a simple fixed-size circular buffer would be more than enough. Just store the command value in a byte array. When there is no incoming data, go through the array and do things

An attiny85 has very limited memory. That's an even better reason to just stick to a char array and not use that wasteful linked list.