From b6137b58d5390cb13114d4d5c96fdb1f1cb9c74e Mon Sep 17 00:00:00 2001 From: Arkadiusz Bokowy Date: Thu, 29 Feb 2024 11:27:13 +0100 Subject: [PATCH] Fix iterator initialization for AdapterIterator --- src/platform/Linux/bluez/AdapterIterator.cpp | 4 ++-- src/platform/Linux/bluez/BluezObjectList.h | 22 +++++++++++++------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/platform/Linux/bluez/AdapterIterator.cpp b/src/platform/Linux/bluez/AdapterIterator.cpp index cd675e5a46b063..ced172446882b8 100644 --- a/src/platform/Linux/bluez/AdapterIterator.cpp +++ b/src/platform/Linux/bluez/AdapterIterator.cpp @@ -41,8 +41,8 @@ CHIP_ERROR AdapterIterator::Initialize() VerifyOrReturnError(mManager, CHIP_ERROR_INTERNAL, ChipLogError(DeviceLayer, "Failed to get D-Bus object manager for listing adapters: %s", error->message)); - mObjectList = BluezObjectList(mManager.get()); - mIterator = mObjectList.begin(); + mObjectList.Init(mManager.get()); + mIterator = mObjectList.begin(); return CHIP_NO_ERROR; } diff --git a/src/platform/Linux/bluez/BluezObjectList.h b/src/platform/Linux/bluez/BluezObjectList.h index 4359d669b65fde..5831f07a3c6c85 100644 --- a/src/platform/Linux/bluez/BluezObjectList.h +++ b/src/platform/Linux/bluez/BluezObjectList.h @@ -35,20 +35,26 @@ namespace Internal { class BluezObjectList { public: - explicit BluezObjectList(GDBusObjectManager * manager) { Initialize(manager); } + BluezObjectList() = default; + explicit BluezObjectList(GDBusObjectManager * manager) { Init(manager); } - ~BluezObjectList() { g_list_free_full(mObjectList, g_object_unref); } - - BluezObjectIterator begin() const { return BluezObjectIterator(mObjectList); } - static BluezObjectIterator end() { return BluezObjectIterator(); } + ~BluezObjectList() + { + if (mObjectList != nullptr) + g_list_free_full(mObjectList, g_object_unref); + } -protected: - void Initialize(GDBusObjectManager * manager) + CHIP_ERROR Init(GDBusObjectManager * manager) { - VerifyOrReturn(manager != nullptr, ChipLogError(DeviceLayer, "Manager is NULL in %s", __func__)); + VerifyOrReturnError(manager != nullptr, CHIP_ERROR_INVALID_ARGUMENT, + ChipLogError(DeviceLayer, "Manager is NULL in %s", __func__)); mObjectList = g_dbus_object_manager_get_objects(manager); + return CHIP_NO_ERROR; } + BluezObjectIterator begin() const { return BluezObjectIterator(mObjectList); } + static BluezObjectIterator end() { return BluezObjectIterator(); } + private: GList * mObjectList = nullptr; };