Skip to content

Software SPI Memory Leak #312

@Jef-GB

Description

@Jef-GB

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.

SW_SPIClass *SW_SPI_Obj = new SW_SPIClass(pinMOSI, pinMISO, pinSCK);

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;
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions