Skip to content

[Linux/Tizen] Replace g_free usages with GAutoPtr #39151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
May 29, 2025
Merged
22 changes: 9 additions & 13 deletions src/platform/Linux/bluez/BluezEndpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ CHIP_ERROR BluezEndpoint::RegisterGattApplicationImpl()
GVariant * options = g_variant_builder_end(&optionsBuilder);

bluez_gatt_manager1_call_register_application(
gattMgr.get(), mpRootPath, options, nullptr,
gattMgr.get(), mpRootPath.get(), options, nullptr,
+[](GObject * aObj, GAsyncResult * aResult, void * self) {
reinterpret_cast<BluezEndpoint *>(self)->RegisterGattApplicationDone(aObj, aResult);
},
Expand Down Expand Up @@ -335,8 +335,8 @@ void BluezEndpoint::HandleNewDevice(BluezDevice1 & aDevice)
VerifyOrExit(conn != nullptr, err = CHIP_ERROR_NO_MEMORY);
SuccessOrExit(err = conn->Init(*this));

mpPeerDevicePath = g_strdup(objectPath);
mConnMap[mpPeerDevicePath] = conn;
mpPeerDevicePath.reset(g_strdup(objectPath));
mConnMap[mpPeerDevicePath.get()] = conn;

ChipLogDetail(DeviceLayer, "New BLE connection: conn=%p device=%s path=%s", conn, conn->GetPeerAddress(), objectPath);

Expand Down Expand Up @@ -369,9 +369,9 @@ BluezGattService1 * BluezEndpoint::CreateGattService(const char * aUUID)
BluezObjectSkeleton * object;
BluezGattService1 * service;

mpServicePath = g_strdup_printf("%s/service", mpRootPath);
ChipLogDetail(DeviceLayer, "CREATE service object at %s", mpServicePath);
object = bluez_object_skeleton_new(mpServicePath);
mpServicePath.reset(g_strdup_printf("%s/service", mpRootPath.get()));
ChipLogDetail(DeviceLayer, "CREATE service object at %s", mpServicePath.get());
object = bluez_object_skeleton_new(mpServicePath.get());

service = bluez_gatt_service1_skeleton_new();
bluez_gatt_service1_set_uuid(service, aUUID);
Expand All @@ -394,7 +394,7 @@ BluezConnection * BluezEndpoint::GetBluezConnection(const char * aPath)

BluezConnection * BluezEndpoint::GetBluezConnectionViaDevice()
{
return GetBluezConnection(mpPeerDevicePath);
return GetBluezConnection(mpPeerDevicePath.get());
}

#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
Expand Down Expand Up @@ -520,8 +520,8 @@ void BluezEndpoint::SetupGattServer(GDBusConnection * aConn)
{
VerifyOrReturn(!mIsCentral);

mpRootPath = g_strdup_printf("/chipoble/%04x", getpid() & 0xffff);
mRoot.reset(g_dbus_object_manager_server_new(mpRootPath));
mpRootPath.reset(g_strdup_printf("/chipoble/%04x", getpid() & 0xffff));
mRoot.reset(g_dbus_object_manager_server_new(mpRootPath.get()));

SetupGattService();

Expand Down Expand Up @@ -586,10 +586,6 @@ void BluezEndpoint::Shutdown()
},
this);

g_free(mpRootPath);
g_free(mpServicePath);
g_free(mpPeerDevicePath);

mIsInitialized = false;
}

Expand Down
6 changes: 3 additions & 3 deletions src/platform/Linux/bluez/BluezEndpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ class BluezEndpoint : public BluezObjectManagerAdapterNotificationsDelegate
bool mIsInitialized = false;

// Paths for objects published by this service
char * mpRootPath = nullptr;
char * mpServicePath = nullptr;
GAutoPtr<char> mpRootPath;
GAutoPtr<char> mpServicePath;

// Objects (interfaces) published by this service
GAutoPtr<GDBusObjectManagerServer> mRoot;
Expand All @@ -133,7 +133,7 @@ class BluezEndpoint : public BluezObjectManagerAdapterNotificationsDelegate

std::unordered_map<std::string, BluezConnection *> mConnMap;
GAutoPtr<GCancellable> mConnectCancellable;
char * mpPeerDevicePath = nullptr;
GAutoPtr<char> mpPeerDevicePath;

// Allow BluezConnection to access our private members
friend class BluezConnection;
Expand Down
11 changes: 4 additions & 7 deletions src/platform/Tizen/DnssdImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
// Note: Include dns-sd-internal.h is needed, this source file uses some undocumented functions.
#include <dns-sd-internal.h>
#include <dns-sd.h>
#include <glib-object.h>
#include <glib.h>

#include <inet/InetConfig.h>
Expand Down Expand Up @@ -336,7 +337,8 @@ void OnResolve(dnssd_error_e result, dnssd_service_h service, void * userData)
err == CHIP_NO_ERROR,
ChipLogError(DeviceLayer, "chip::Inet::InterfaceId::InterfaceNameToId() failed: %" CHIP_ERROR_FORMAT, err.Format()));

ret = dnssd_service_get_all_txt_record(service, &rCtx->mResultTxtRecordLen, reinterpret_cast<void **>(&rCtx->mResultTxtRecord));
ret = dnssd_service_get_all_txt_record(service, &rCtx->mResultTxtRecordLen,
reinterpret_cast<void **>(rCtx->mResultTxtRecord.out()));
VerifyOrExit(ret == DNSSD_ERROR_NONE,
ChipLogError(DeviceLayer, "dnssd_service_get_all_txt_record() failed: %s", get_error_message(ret)));

Expand Down Expand Up @@ -462,19 +464,14 @@ ResolveContext::ResolveContext(DnssdTizen * instance, const char * name, const c
mCbContext = context;
}

ResolveContext::~ResolveContext()
{
g_free(mResultTxtRecord);
}

void ResolveContext::Finalize(CHIP_ERROR error)
{
ChipLogProgress(DeviceLayer, "DNSsd %s", __func__);
// In case of error, run the callback function with nullptr as the result.
VerifyOrReturn(error == CHIP_NO_ERROR, mCallback(mCbContext, nullptr, chip::Span<chip::Inet::IPAddress>(), error));

std::vector<chip::Dnssd::TextEntry> textEntries;
GetTextEntries(mResultTxtRecordLen, mResultTxtRecord, textEntries);
GetTextEntries(mResultTxtRecordLen, mResultTxtRecord.get(), textEntries);
mResult.mTextEntries = textEntries.empty() ? nullptr : textEntries.data();
mResult.mTextEntrySize = textEntries.size();

Expand Down
6 changes: 4 additions & 2 deletions src/platform/Tizen/DnssdImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@
#include <vector>

#include <dns-sd.h>
#include <glib-object.h>
#include <glib.h>

#include <inet/IPAddress.h>
#include <inet/InetInterface.h>
#include <lib/core/CHIPError.h>
#include <lib/dnssd/Constants.h>
#include <lib/dnssd/platform/Dnssd.h>
#include <platform/GLibTypeDeleter.h>

namespace chip {
namespace Dnssd {
Expand Down Expand Up @@ -106,8 +108,8 @@ struct ResolveContext : public GenericContext
bool mIsResolving = false;

// Resolved service
DnssdService mResult = {};
uint8_t * mResultTxtRecord = nullptr;
DnssdService mResult = {};
GAutoPtr<uint8_t> mResultTxtRecord;
unsigned short mResultTxtRecordLen = 0;

ResolveContext(DnssdTizen * instance, const char * name, const char * type, uint32_t interfaceId, DnssdResolveCallback callback,
Expand Down
Loading