From 8b932e3a3c4de39aaef86109b3ab5f0391bc5267 Mon Sep 17 00:00:00 2001 From: rodolpheh Date: Wed, 8 Nov 2023 22:47:41 +0000 Subject: [PATCH] Try to improve stability in editor The more I made changes, the more it crashed. Got it to a point where it doesn't crash when starting the editor, but it will crash when exciting. --- demo/scripts/HUD.gd | 3 ++- src/device.cpp | 1 + src/device_node.cpp | 6 ++++-- src/frame.cpp | 4 ++-- src/frame.h | 7 ++----- src/hand.cpp | 2 +- src/hand.h | 2 +- src/ultraleap.cpp | 41 +++++++++++++++++++++++------------------ src/ultraleap.h | 17 +++++++---------- 9 files changed, 43 insertions(+), 40 deletions(-) diff --git a/demo/scripts/HUD.gd b/demo/scripts/HUD.gd index 522d8d3..24bad54 100644 --- a/demo/scripts/HUD.gd +++ b/demo/scripts/HUD.gd @@ -24,7 +24,8 @@ func _ready(): func _physics_process(_delta): if Time.get_unix_time_from_system() - latest_update > 0.5: var frame : UltraleapFrame = tracking.get_last_frame() - $Footer/TrackingFramerate.text = "Tracking Frame Rate: " + str(ceil(frame.framerate)) + if frame != null: + $Footer/TrackingFramerate.text = "Tracking Frame Rate: " + str(ceil(frame.framerate)) latest_update = Time.get_unix_time_from_system() diff --git a/src/device.cpp b/src/device.cpp index d0223d5..08dfc83 100644 --- a/src/device.cpp +++ b/src/device.cpp @@ -150,6 +150,7 @@ void UltraleapDevice::open() { void UltraleapDevice::close() { LeapCloseDevice(device); + LeapDestroyClockRebaser(rebaser); } void UltraleapDevice::on_frame_received(const LEAP_TRACKING_EVENT* frame) { diff --git a/src/device_node.cpp b/src/device_node.cpp index 66504a2..ef8e44d 100644 --- a/src/device_node.cpp +++ b/src/device_node.cpp @@ -192,8 +192,10 @@ void UltraleapDeviceNode::set_tracker(NodePath value) { void UltraleapDeviceNode::_notification(int p_what) { if (p_what == Node::NOTIFICATION_READY) { - UltraleapHandTracking* tracking = Object::cast_to(get_node_or_null(tracker)); - if (tracking != NULL) { + Node *tracking_node = get_node_or_null(tracker); + if (tracking_node != NULL) { + UltraleapHandTracking* tracking = Object::cast_to(tracking_node); + // Connect to tracking signals tracking->connect("device_added", Callable((Object*)this, "device_added")); tracking->connect("device_removed", Callable((Object*)this, "device_removed")); diff --git a/src/frame.cpp b/src/frame.cpp index c245930..3635a1d 100644 --- a/src/frame.cpp +++ b/src/frame.cpp @@ -89,11 +89,11 @@ void UltraleapFrame::fill_frame_data(Ref ul_frame, const LEAP_TR for (size_t i = 0; i < frame->nHands; i++) { if (frame->pHands[i].type == eLeapHandType_Left) { - UltraleapHand::fill_hand_data(ul_frame->left_hand, &frame->pHands[i]); + UltraleapHand::fill_hand_data(ul_frame->left_hand_ref, &frame->pHands[i]); ul_frame->is_left_hand_visible = true; } else { - UltraleapHand::fill_hand_data(ul_frame->right_hand, &frame->pHands[i]); + UltraleapHand::fill_hand_data(ul_frame->right_hand_ref, &frame->pHands[i]); ul_frame->is_right_hand_visible = true; } } diff --git a/src/frame.h b/src/frame.h index 8165ef2..7d244d9 100644 --- a/src/frame.h +++ b/src/frame.h @@ -21,9 +21,6 @@ class UltraleapFrame : public Resource { public: uint32_t id; - UltraleapHand* left_hand = memnew(UltraleapHand); - UltraleapHand* right_hand = memnew(UltraleapHand); - bool is_left_hand_visible = false; bool is_right_hand_visible = false; @@ -49,8 +46,8 @@ protected: static void _bind_methods(); private: - Ref left_hand_ref = Ref(left_hand); - Ref right_hand_ref = Ref(right_hand); + Ref left_hand_ref = Ref(memnew(UltraleapHand)); + Ref right_hand_ref = Ref(memnew(UltraleapHand)); }; #endif \ No newline at end of file diff --git a/src/hand.cpp b/src/hand.cpp index b4d6fae..6c46720 100644 --- a/src/hand.cpp +++ b/src/hand.cpp @@ -223,7 +223,7 @@ void UltraleapHand::_bind_methods() { ); }; -void UltraleapHand::fill_hand_data(UltraleapHand* ul_hand, LEAP_HAND* hand) { +void UltraleapHand::fill_hand_data(Ref ul_hand, LEAP_HAND* hand) { ul_hand->type = hand->type == eLeapHandType_Left ? UltraleapTypes::Chirality::Left : UltraleapTypes::Chirality::Right; ul_hand->confidence = hand->confidence; ul_hand->visible_time = hand->visible_time; diff --git a/src/hand.h b/src/hand.h index ed4b3a5..70484d7 100644 --- a/src/hand.h +++ b/src/hand.h @@ -95,7 +95,7 @@ public: } void set_digits(Array digits) { digits_array = digits; } - static void fill_hand_data(UltraleapHand* ul_hand, LEAP_HAND* hand); + static void fill_hand_data(Ref ul_hand, LEAP_HAND* hand); protected: static void _bind_methods(); diff --git a/src/ultraleap.cpp b/src/ultraleap.cpp index 8d4bd63..e3b4cb2 100644 --- a/src/ultraleap.cpp +++ b/src/ultraleap.cpp @@ -25,10 +25,26 @@ using namespace godot; +UltraleapHandTracking::UltraleapHandTracking() { + UtilityFunctions::print("Initializing UltraleapHandTracking"); + + #if ANDROID_ENABLED + // Get and store the binder + if (Engine::get_singleton()->has_singleton("UltraleapBinder")) { + binder = Engine::get_singleton()->get_singleton("UltraleapBinder"); + } + #endif + + // For now we will also start automatically if we're in the editor, so that we can have data for tool scripts + if (autostart || Engine::get_singleton()->is_editor_hint()) { + start(); + } +} + UltraleapHandTracking::~UltraleapHandTracking() { UtilityFunctions::print("Destroying UltraleapHandTracking"); - if (is_running) { + if (is_running || messageLoop.joinable()) { UtilityFunctions::print("Stopping the tracking thread"); keep_running = false; messageLoop.join(); @@ -39,6 +55,8 @@ UltraleapHandTracking::~UltraleapHandTracking() { #if ANDROID_ENABLED binder = NULL; #endif + + UtilityFunctions::print("UltraleapHandTracking destroyed"); } void UltraleapHandTracking::dispose_ultraleap() { @@ -61,6 +79,8 @@ void UltraleapHandTracking::dispose_ultraleap() { } } #endif + + UtilityFunctions::print("Ultraleap was disposed of"); } void UltraleapHandTracking::_bind_methods() { @@ -77,7 +97,7 @@ void UltraleapHandTracking::_bind_methods() { ClassDB::bind_method(D_METHOD("is_connected"), &UltraleapHandTracking::is_connected); ClassDB::bind_method(D_METHOD("is_started"), &UltraleapHandTracking::is_started); - ClassDB::bind_method(D_METHOD("set_last_frame", "last_frame"), &UltraleapHandTracking::set_last_frame); + //ClassDB::bind_method(D_METHOD("set_last_frame", "last_frame"), &UltraleapHandTracking::set_last_frame); ClassDB::bind_method(D_METHOD("get_last_frame"), &UltraleapHandTracking::get_last_frame); ClassDB::bind_method(D_METHOD("set_tracking_mode", "tracking_mode"), &UltraleapHandTracking::set_tracking_mode, DEFVAL(0)); @@ -118,7 +138,7 @@ void UltraleapHandTracking::_bind_methods() { "UltraleapFrame", PROPERTY_USAGE_READ_ONLY | PROPERTY_USAGE_EDITOR ), - "set_last_frame", + "", "get_last_frame" ); @@ -387,7 +407,6 @@ void UltraleapHandTracking::serviceMessageLoop() { LEAP_CONNECTION_MESSAGE msg; eLeapRS result; - keep_running = true; is_running = true; while (keep_running) { @@ -774,19 +793,6 @@ void UltraleapHandTracking::_notification(int p_what) { events.pop(); } } - else if (p_what == Node::NOTIFICATION_READY) { - #if ANDROID_ENABLED - // Get and store the binder - if (Engine::get_singleton()->has_singleton("UltraleapBinder")) { - binder = Engine::get_singleton()->get_singleton("UltraleapBinder"); - } - #endif - - // For now we will also start automatically if we're in the editor, so that we can have data for tool scripts - if (autostart || Engine::get_singleton()->is_editor_hint()) { - start(); - } - } } String UltraleapHandTracking::generate_connection_payload() { @@ -805,7 +811,6 @@ void UltraleapHandTracking::set_primary_device(Ref device) { } // Pretty hack-ish, we open the device and re-store the handle in the UltraleapDevice object LeapOpenDevice(device->device_ref, &(device->device)); - LeapCreateClockRebaser(&(device->rebaser)); primary_device = device; diff --git a/src/ultraleap.h b/src/ultraleap.h index b41c854..4ba8ae8 100644 --- a/src/ultraleap.h +++ b/src/ultraleap.h @@ -27,6 +27,7 @@ class UltraleapHandTracking : public Node3D { GDCLASS(UltraleapHandTracking, Node3D); public: + UltraleapHandTracking(); ~UltraleapHandTracking(); void start(); @@ -39,11 +40,11 @@ public: bool get_interpolate(); void set_interpolate(bool value); - UltraleapDeviceList* devices = memnew(UltraleapDeviceList); + Ref devices = Ref(memnew(UltraleapDeviceList)); - Ref last_frame_ref = Ref(memnew(UltraleapFrame)); - Ref left_image_ref = Ref(memnew(UltraleapImage)); - Ref right_image_ref = Ref(memnew(UltraleapImage)); + Ref last_frame_ref; + Ref left_image_ref; + Ref right_image_ref; String service_ip = String("127.0.0.1"); uint32_t service_port = 12345; @@ -100,13 +101,10 @@ protected: static void _bind_methods(); private: - bool _isRunning = false; - - std::atomic is_running = false; - std::atomic keep_running = false; + std::atomic_bool is_running = ATOMIC_VAR_INIT(false); + std::atomic_bool keep_running = ATOMIC_VAR_INIT(true); LEAP_CONNECTION connectionHandle = NULL; - LEAP_CLOCK_REBASER clockSynchronizer; std::thread messageLoop; @@ -115,7 +113,6 @@ private: bool connected = false; /* We have confirmed connection */ Ref primary_device = NULL; - typedef struct { StringName signal; Variant arg;