Skip to content

Commit 4dae7a1

Browse files
committed
ubuntu: Make sure GPIO map doesn't get destroyed on exit
1 parent 9e6efd2 commit 4dae7a1

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

platforms/ubuntu/src/ubuntu_gpio.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,14 @@ struct GPIOPinCtx {
4444
}
4545
};
4646

47-
static std::map<int, std::unique_ptr<GPIOPinCtx>> pins_;
47+
static std::map<int, std::unique_ptr<GPIOPinCtx>> *pins_ = nullptr;
4848

4949
static GPIOPinCtx *GetPinCtx(int pin) {
50-
auto it = pins_.find(pin);
51-
if (it == pins_.end()) return nullptr;
50+
if (pins_ == nullptr) {
51+
pins_ = new std::map<int, std::unique_ptr<GPIOPinCtx>>();
52+
}
53+
auto it = pins_->find(pin);
54+
if (it == pins_->end()) return nullptr;
5255
return it->second.get();
5356
}
5457

@@ -60,7 +63,7 @@ static GPIOPinCtx *GetOrCreatePinCtx(int pin, enum mgos_gpio_mode mode) {
6063
LOG(LL_INFO, ("GPIO: New pin %s, mode %d (%s)", mgos_gpio_str(pin, buf),
6164
mode, (mode == MGOS_GPIO_MODE_INPUT ? "input" : "output")));
6265
ctx = new_ctx.get();
63-
pins_.emplace(pin, std::move(new_ctx));
66+
pins_->emplace(pin, std::move(new_ctx));
6467
}
6568
return ctx;
6669
}

0 commit comments

Comments
 (0)