Skip to content

Commit

Permalink
ubuntu: Make sure GPIO map doesn't get destroyed on exit
Browse files Browse the repository at this point in the history
  • Loading branch information
rojer committed Feb 13, 2021
1 parent 9e6efd2 commit 4dae7a1
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions platforms/ubuntu/src/ubuntu_gpio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,14 @@ struct GPIOPinCtx {
}
};

static std::map<int, std::unique_ptr<GPIOPinCtx>> pins_;
static std::map<int, std::unique_ptr<GPIOPinCtx>> *pins_ = nullptr;

static GPIOPinCtx *GetPinCtx(int pin) {
auto it = pins_.find(pin);
if (it == pins_.end()) return nullptr;
if (pins_ == nullptr) {
pins_ = new std::map<int, std::unique_ptr<GPIOPinCtx>>();
}
auto it = pins_->find(pin);
if (it == pins_->end()) return nullptr;
return it->second.get();
}

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

0 comments on commit 4dae7a1

Please sign in to comment.