deleting and creating objects explictly

how do we create and delete objects in arduino uno explicitly?

i am using multiple IR sensors and reading them on the same pin (10) of the arduino Uno R3 board.
when im running the program, im facing memory leakage.
so i thought of creating and deleting the objects of the IR classes every time the receive function is called.

void receive() {
  str = "receiving for sensor at pin " + String(i); 
 // prints the i[sup]th[/sup] sensor's readings.
   // i is declared as global variable and is used in for-loop in the "loop()" part of the program

  Serial.println(str);


  IRrecv irrecv(10);
  decode_results results ;
  irrecv.enableIRIn(); // Start the receiver


  if (irrecv.decode(&results))
  {
    
    Serial.println("   " + String(results.value));

    }
    irrecv.resume(); // Receive the next value
  }
  results.value = 0;


  free(irrecv);    //delete the receiver object
  free(results);   //delete the results object

}

when i compile, i get an error like this:

error: cannot convert 'decode_results' to 'void*' for argument '1' to 'void free(void*)'

free(results);

any suggestions?

I don't know that you need to free them. The way you are accessing them makes it look like they are not pointers but real objects. So they'll be destroyed for you when they go out of scope. Can you show the whole code?

Post the whole code.
There won't be memory leaks unless you continually use 'new', malloc, or use String.

Delta_G:
I don’t know that you need to free them. The way you are accessing them makes it look like they are not pointers but real objects. So they’ll be destroyed for you when they go out of scope. Can you show the whole code?

u sure about that? objects memory getting released once they go out of scope?

the code

#include <boarddefs.h>
#include <IRremote.h>
#include <IRremoteInt.h>

String str = "";
void activate();

void activate(int pin) {

  IRrecv irrecv(10);
  decode_results results ;
  irrecv.enableIRIn(); // Start the receiver

  digitalWrite(pin, HIGH);

  if (irrecv.decode(&results))
  {
    //Serial.print("      ");
    Serial.println(results.value);
    irrecv.resume(); // Receive the next value
  }
  str = "ended recieving for sensor at pin " + String(pin) ;
  //results.value = 0;
  // Serial.println(str);

  digitalWrite(pin, LOW);

}

void setup() {

  Serial.begin(9600);
  pinMode(2, OUTPUT);  //power supply to1 sensor
  pinMode(3, OUTPUT);  //power supply to 2nd  sensor
  pinMode(10, INPUT);  //common input pin for all the recievers
}

void loop() {
  // put your main code here, to run repeatedly:
  for (i = 2; i < 4; i++) {

    str = "ACTIVE PIN " + String(i);
    Serial.println(str);

    activate(i);
    Serial.println();

  }

}

my plan is to supply power to the sensors using the for-loop in the loop() function, sending the command to activate(int pin) method which sets appropriate pin HIGH…
it is working though…

when the pin 3 is activated, but there is no sensor at pin 3, the serial monitor displays results for pin 3…

any idea how to fix it???
i was earlier using the all variables as global, now i moved to parameterised methods and still the same happens…

iwbatz:
u sure about that? objects memory getting released once they go out of scope?

I am absolutely positive about it. Since you never use new or malloc in your code you have no use for free.

iwbatz:
when the pin 3 is activated, but there is no sensor at pin 3, the serial monitor displays results for pin 3...

It's not clear what you mean here. What sensors? You seem to assume that we know something about your project that you haven't shown yet. Namely what is connected and how. But I have to ask, how will Arduino know whether or not something is connected to pin 3?