TMCStepper
TMCStepper copied to clipboard
Software SPI Memory Leak
Issue
Calling of the constructors for the TMC2130 that create the software SPI instance create it using new. As there is no destructor specified for the class, deleting the TMC2130 class instance will leave you with a memory leak. The software SPI variable TMC_SW_SPI is never deleted, only set to nullptr on class instantiation.
https://github.com/teemuatlut/TMCStepper/blob/74e8e6881adc9241c2e626071e7328d7652f361a/src/source/TMC2130Stepper.cpp#L23
Solution
Create a destructor for the TMC2130 class that checks if the instance exists and deletes it when it does.
## .\TMCStepper.h
virtual ~TMC2130Stepper()
## .\source\TMC2130Stepper.cpp
TMC2130Stepper::~TMC2130Stepper()
{
if (TMC_SW_SPI != nullptr) {
delete TMC_SW_SPI;
TMC_SW_SPI = nullptr;
}
}