Skip to content

Commit 9e6efd2

Browse files
committed
Fix leak of the GPIO state array
1 parent 9a1170d commit 9e6efd2

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

src/mgos_gpio.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ static IRAM struct mgos_gpio_state *mgos_gpio_get_state(int pin) {
6363
};
6464

6565
static struct mgos_gpio_state *mgos_gpio_get_or_create_state(int pin) {
66-
struct mgos_gpio_state *s = mgos_gpio_get_state(pin);
66+
struct mgos_gpio_state *s = mgos_gpio_get_state(pin), *old_s = NULL;
6767
if (s != NULL) return s;
6868
s = (struct mgos_gpio_state *) calloc(s_num_gpio_states + 1, sizeof(*s));
6969
if (s == NULL) return NULL;
@@ -73,10 +73,12 @@ static struct mgos_gpio_state *mgos_gpio_get_or_create_state(int pin) {
7373
if (s_state != NULL) {
7474
memcpy(s, s_state, s_num_gpio_states * sizeof(*s));
7575
}
76+
old_s = s_state;
7677
s_state = s;
7778
s = &s_state[s_num_gpio_states++];
7879
s->pin = pin;
7980
mgos_ints_enable();
81+
free(old_s);
8082
return s;
8183
}
8284

0 commit comments

Comments
 (0)