Skip to content

Commit

Permalink
fix segfault on return activity_main
Browse files Browse the repository at this point in the history
  • Loading branch information
loki-47-6F-64 committed Sep 21, 2018
1 parent 610b71d commit d39587f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
5 changes: 5 additions & 0 deletions kitty/kitty/util/move_by_copy.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,10 @@ MoveByCopy<T> cmove(T &movable) {
return MoveByCopy<T>(std::move(movable));
}

// Do NOT use this unless you are absolutely certain the object to be moved is no longer necessary by the caller
template<class T>
MoveByCopy<T> const_cmove(const T &movable) {
return MoveByCopy<T>(std::move(const_cast<T&>(movable)));
}
}
#endif
39 changes: 26 additions & 13 deletions modules/bluecast/native/cpp/bluecast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ void BlueCallback::on_scan_result(const gen::BlueScanResult &scan) {
if(it != _blue_beacons.end() && it->second.beacon.device.name != dev.name) {
it->second.beacon.device = std::move(dev);

_blue_view_main_callback->get_blue_view_controller()->beacon_list_update(it->second.beacon);
if(_blue_view_main_callback) {
_blue_view_main_callback->get_blue_view_controller()->beacon_list_update(it->second.beacon);
}
}
}, util::cmove(const_cast<gen::BlueScanResult&>(scan)));
}
Expand Down Expand Up @@ -138,8 +140,9 @@ void BlueCallback::on_characteristic_read(const std::shared_ptr<gen::BlueGatt> &
log(gen::LogSeverity::INFO, "value::" + characteristic->get_string_value(0));
}


_blue_view_display_callback->blue_view_display_controller()->display(_blue_view_display_callback->get_device(),characteristic->get_string_value(0));
if(_blue_view_display_callback) {
_blue_view_display_callback->blue_view_display_controller()->display(_blue_view_display_callback->get_device(), characteristic->get_string_value(0));
}
}

std::shared_ptr<gen::BlueViewMainCallback> BlueCallback::on_create_main(
Expand All @@ -151,6 +154,10 @@ std::shared_ptr<gen::BlueViewMainCallback> BlueCallback::on_create_main(
_blue_view_main_callback = std::make_shared<BlueViewMainCallback>(blue_view, permission_manager);

tasks().push([this]() {
if(!_blue_view_main_callback) {
return;
}

auto &control = _blue_view_main_callback->get_blue_view_controller();
for(const auto &beacon : _blue_beacons) {
control->beacon_list_update(beacon.second.beacon);
Expand All @@ -175,31 +182,37 @@ std::shared_ptr<gen::BlueViewDisplayCallback> BlueCallback::on_create_display(

void BlueCallback::on_destroy_main() {
log(gen::LogSeverity::DEBUG, "on_destroy_main");
if(_blue_view_main_callback->scan_enabled()) {
TASK(,blueManager()->scan(false));
}

_blue_view_main_callback.reset();
TASK(this,
if(_blue_view_main_callback->scan_enabled()) {
blueManager()->scan(false);
}

_blue_view_main_callback.reset();
);
}

void BlueCallback::on_destroy_display() {
log(gen::LogSeverity::DEBUG, "on_destroy_display");
_blue_view_display_callback.reset();
TASK(this, _blue_view_display_callback.reset());
}

void BlueCallback::on_beacon_update(const gen::BlueBeacon &beacon) {
auto &_beacon = const_cast<gen::BlueBeacon&>(beacon);

tasks().push([this](gen::BlueBeacon &&beacon) {
_blue_view_main_callback->get_blue_view_controller()->beacon_list_update(beacon);
if(_blue_view_main_callback) {
_blue_view_main_callback->get_blue_view_controller()->beacon_list_update(beacon);
}

auto it = _blue_beacons.find(beacon.device.address);

util::TaskPool::task_id_t task_id;
// If beacon first appears...
if(it == _blue_beacons.cend()) {
auto delayed_task = tasks().pushTimed([this](const auto &beacon) {
_blue_view_main_callback->get_blue_view_controller()->beacon_list_remove(beacon);

if(_blue_view_main_callback) {
_blue_view_main_callback->get_blue_view_controller()->beacon_list_remove(beacon);
}

_blue_beacons.erase(beacon.address);
}, std::chrono::seconds(3), beacon.device);
Expand All @@ -214,7 +227,7 @@ void BlueCallback::on_beacon_update(const gen::BlueBeacon &beacon) {
std::string address = beacon.device.address;
_blue_beacons.emplace(address, beacon_t { std::move(beacon), task_id });

}, util::cmove(_beacon));
}, util::const_cmove(beacon));
}

BlueCallback::~BlueCallback() = default;
Expand Down

0 comments on commit d39587f

Please sign in to comment.