Skip to content

Commit 279baa7

Browse files
authored
Merge pull request #29 from mskvortsov/fix-exit-memleak
Fix a gpio memory leak on shutdown
2 parents 3a25662 + a9e9c2d commit 279baa7

File tree

4 files changed

+12
-12
lines changed

4 files changed

+12
-12
lines changed

cores/portduino/PortduinoGPIO.cpp

+7-9
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ int NUM_GPIOS;
1818

1919
bool realHardware = false;
2020

21-
std::vector<GPIOPinIf*> pins;
21+
std::vector<std::unique_ptr<GPIOPinIf>> pins;
2222

23+
GPIOPinIf::~GPIOPinIf() {}
2324

2425
/** By default we assign simulated GPIOs to all pins, later applications can customize this in portduinoSetup */
2526
void gpioInit(int _num_gpios) {
2627
NUM_GPIOS = _num_gpios;
2728
for(size_t i = 0; i < NUM_GPIOS; i++)
28-
pins.push_back( new SimGPIOPin(i, "Unbound"));
29+
pins.push_back(std::make_unique<SimGPIOPin>(i, "Unbound"));
2930
}
3031

3132
void gpioIdle() {
@@ -35,20 +36,17 @@ void gpioIdle() {
3536
}
3637

3738
void gpioBind(GPIOPinIf *p) {
38-
assert(p->getPinNum() < NUM_GPIOS);
39-
pins[p->getPinNum()] = p;
40-
realHardware = true;
39+
assert(p->getPinNum() < NUM_GPIOS);
40+
pins[p->getPinNum()].reset(p);
41+
realHardware = true;
4142
}
4243

4344
/**
4445
* Return the specified GPIO pin or the UnboundPin pin instance */
4546
GPIOPinIf *getGPIO(pin_size_t n)
4647
{
4748
assert(n < NUM_GPIOS);
48-
auto p = pins[n];
49-
50-
assert(p);
51-
return p;
49+
return pins[n].get();
5250
}
5351

5452
void pinMode(pin_size_t pinNumber, PinMode pinMode)

cores/portduino/PortduinoGPIO.h

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
class GPIOPinIf
1616
{
1717
public:
18+
virtual ~GPIOPinIf() = 0;
1819
virtual pin_size_t getPinNum() const = 0;
1920

2021
/** Called to read from a pin and if the pin has changed state possibly call an ISR, also changes

cores/portduino/linux/gpio/LinuxGPIOPin.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ static struct gpiod_chip *chip_open_by_name(const char *name)
104104
* Try to find the specified linux gpio line, throw exception if not found
105105
*/
106106
gpiod_line *LinuxGPIOPin::getLine(const char *chipLabel, const char *linuxPinName) {
107-
struct gpiod_chip *chip;
108107
std::string path = "/dev/";
109108
path += chipLabel;
110109
if (access(path.c_str(), R_OK) == 0) {
@@ -181,7 +180,6 @@ gpiod_line *LinuxGPIOPin::getLine(const char *chipLabel, const char *linuxPinNam
181180
* Try to find the specified linux gpio line, throw exception if not found
182181
*/
183182
gpiod_line *LinuxGPIOPin::getLine(const char *chipLabel, const int linuxPinNum) {
184-
struct gpiod_chip *chip;
185183
std::string path = "/dev/";
186184
path += chipLabel;
187185
if (access(path.c_str(), R_OK) == 0) {
@@ -270,7 +268,7 @@ LinuxGPIOPin::LinuxGPIOPin(pin_size_t n, const char *chipLabel,
270268

271269
LinuxGPIOPin::~LinuxGPIOPin() {
272270
gpiod_line_release(line);
273-
// FIXME must call gpiod_chip_unref(chip);
271+
gpiod_chip_close(chip);
274272
}
275273

276274
/// Read the low level hardware for this pin

cores/portduino/linux/gpio/LinuxGPIOPin.h

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class LinuxGPIOPin : public GPIOPin {
2929
/// Our GPIO line
3030
struct gpiod_line *line;
3131

32+
/// Chip structure associated with the line
33+
struct gpiod_chip *chip;
34+
3235
public:
3336

3437
/**

0 commit comments

Comments
 (0)