Skip to content

Commit

Permalink
Use ScopedResource for builder
Browse files Browse the repository at this point in the history
  • Loading branch information
sfodagain committed Sep 11, 2024
1 parent 6096eed commit bd1b0d4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
9 changes: 5 additions & 4 deletions samples/fleet_provisioning/mqtt5_fleet_provisioning/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,13 @@ struct RegisterThingContext
std::shared_ptr<Aws::Crt::Mqtt5::Mqtt5Client> createMqtt5Client(const Utils::cmdData &cmdData, Mqtt5ClientContext &ctx)
{
// Create the MQTT5 builder and populate it with data from cmdData.
Aws::Iot::Mqtt5ClientBuilder *builder = Aws::Iot::Mqtt5ClientBuilder::NewMqtt5ClientBuilderWithMtlsFromPath(
cmdData.input_endpoint, cmdData.input_cert.c_str(), cmdData.input_key.c_str());
auto builder = Aws::Crt::ScopedResource<Aws::Iot::Mqtt5ClientBuilder>(
Aws::Iot::Mqtt5ClientBuilder::NewMqtt5ClientBuilderWithMtlsFromPath(
cmdData.input_endpoint, cmdData.input_cert.c_str(), cmdData.input_key.c_str()),
[](Aws::Iot::Mqtt5ClientBuilder *ptr) { delete ptr; });

// Check if the builder setup correctly.
if (builder == nullptr)
if (!builder)
{
printf(
"Failed to setup mqtt5 client builder with error code %d: %s", LastError(), ErrorDebugString(LastError()));
Expand Down Expand Up @@ -128,7 +130,6 @@ std::shared_ptr<Aws::Crt::Mqtt5::Mqtt5Client> createMqtt5Client(const Utils::cmd

// Create Mqtt5Client
std::shared_ptr<Aws::Crt::Mqtt5::Mqtt5Client> client = builder->Build();
delete builder;

return client;
}
Expand Down
10 changes: 6 additions & 4 deletions servicetests/tests/FleetProvisioning/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,14 @@ struct RegisterThingContext
std::shared_ptr<Aws::Crt::Mqtt5::Mqtt5Client> createMqtt5Client(const Utils::cmdData &cmdData, Mqtt5ClientContext &ctx)
{
// Create the MQTT5 builder and populate it with data from cmdData.
Aws::Iot::Mqtt5ClientBuilder *builder = Aws::Iot::Mqtt5ClientBuilder::NewMqtt5ClientBuilderWithMtlsFromPath(
cmdData.input_endpoint, cmdData.input_cert.c_str(), cmdData.input_key.c_str());
// Create the MQTT5 builder and populate it with data from cmdData.
auto builder = Aws::Crt::ScopedResource<Aws::Iot::Mqtt5ClientBuilder>(
Aws::Iot::Mqtt5ClientBuilder::NewMqtt5ClientBuilderWithMtlsFromPath(
cmdData.input_endpoint, cmdData.input_cert.c_str(), cmdData.input_key.c_str()),
[](Aws::Iot::Mqtt5ClientBuilder *ptr) { delete ptr; });

// Check if the builder setup correctly.
if (builder == nullptr)
if (!builder)
{
printf(
"Failed to setup mqtt5 client builder with error code %d: %s", LastError(), ErrorDebugString(LastError()));
Expand Down Expand Up @@ -130,7 +133,6 @@ std::shared_ptr<Aws::Crt::Mqtt5::Mqtt5Client> createMqtt5Client(const Utils::cmd

// Create Mqtt5Client
auto client = builder->Build();
delete builder;

fprintf(stdout, "Connecting...\n");
if (!client->Start())
Expand Down

0 comments on commit bd1b0d4

Please sign in to comment.