madhephaestus / ESP32Servo

Arduino-compatible servo library for the ESP32
139 stars 55 forks source link

Fix bug in allocation algorithm #16

Closed FedericoBusero closed 4 years ago

FedericoBusero commented 4 years ago

When some time ago, the de-allocation of an ESP32PWM object was introduced, the allocation algorithm wasn't updated. As a result the allocation algorithm is no longer correct as it will not reuse a previous de-allocated channel.

This causes leaking memory, channels and finally stopping the program when using e.g. lots of analogWrite with zero and non-zero values because analogWrite with value 0 calls deallocation. But also with Servo objects, allocation will go wrong when deallocating.

The proposed solution will not allocate by simply iterating, but by checking which slot is free. Finally, the random crashes are gone.

example: analogWrite(pin1,100); // allocates the first channel analogWrite(pin2,100); // allocates the second channel analogWrite(pin1,0); // this deallocates the first channel analogWrite(pin1,100); => this should reallocate the first allocated channel, but in reality it overwrites the second channel