Releasing an object?

After I have instantiated a class:

LED led = LED(13);

When I no longer need the led, how do I delete the object to reclaim memory used?

In the case of LED the memory required is minimal (and the hardware is unlikely to change while the sketch is running), but it may be useful for other classes.

Hmm, the opposite of a constructor is a destructor! I'm not sure why but the Arduino flavor of C/++ doesn't support new and delete , nor does it automatically create a destructor. The C function free() works with pointers initialized by malloc(), I'm not so sure about objects. you could try implementing a destructor ~object() function I try to manage memory with objects and structures by limiting their scope to subroutines called from main() so that the memory is available to the other subroutines once control returns. At least this is how I think it works.

Dave, the best solution, as emdee implied, is to create a scope for your instance. For example:

void loop()
{
  if (digitalRead(11) == HIGH)
  {
    LED led = LED(13);
    // do something here with LED
  }
}

This example creates an LED object only when the if statement evaluates to true. As soon as the if block terminates, the LED object is destroyed.

Just for the record, no flavor of C/C++ automatically creates a destructor, and creating one yourself won't solve anything in itself. Destructors are only called when an object is destroyed, and if your LED object is global, it is never destroyed -- not in Arduino land anyway.

Mikal