Skip to content

Commit

Permalink
Use ScopedResource instead of raw pointer in all samples
Browse files Browse the repository at this point in the history
  • Loading branch information
sfodagain committed Sep 11, 2024
1 parent bd1b0d4 commit ed8c9ed
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 22 deletions.
19 changes: 14 additions & 5 deletions samples/device_defender/mqtt5_basic_report/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,20 @@ int main(int argc, char *argv[])
Utils::cmdData cmdData = Utils::parseSampleInputDeviceDefender(argc, argv, &apiHandle);

// Create the MQTT builder and populate it with data from cmdData.
auto clientConfigBuilder = Aws::Iot::Mqtt5ClientBuilder::NewMqtt5ClientBuilderWithMtlsFromPath(
cmdData.input_endpoint, cmdData.input_cert.c_str(), cmdData.input_key.c_str());
auto clientConfigBuilder = 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; });
if (clientConfigBuilder == nullptr)
{
fprintf(
stdout,
"Failed to setup MQTT5 client builder with error code %d: %s",
LastError(),
ErrorDebugString(LastError()));
return -1;
}

if (cmdData.input_ca != "")
{
clientConfigBuilder->WithCertificateAuthority(cmdData.input_ca.c_str());
Expand Down Expand Up @@ -119,9 +131,6 @@ int main(int argc, char *argv[])
// Create Mqtt5Client
std::shared_ptr<Aws::Crt::Mqtt5::Mqtt5Client> client = clientConfigBuilder->Build();

// Clean up the builder
delete clientConfigBuilder;

if (client == nullptr)
{
fprintf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ std::shared_ptr<Aws::Crt::Mqtt5::Mqtt5Client> createMqtt5Client(const Utils::cmd
[](Aws::Iot::Mqtt5ClientBuilder *ptr) { delete ptr; });

// Check if the builder setup correctly.
if (!builder)
if (builder == nullptr)
{
printf(
"Failed to setup mqtt5 client builder with error code %d: %s", LastError(), ErrorDebugString(LastError()));
Expand Down
7 changes: 4 additions & 3 deletions samples/jobs/mqtt5_job_execution/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ int main(int argc, char *argv[])
Utils::cmdData cmdData = Utils::parseSampleInputJobs(argc, argv, &apiHandle);

// 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)
Expand Down Expand Up @@ -101,7 +103,6 @@ int main(int argc, char *argv[])

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

fprintf(stdout, "Connecting...\n");
Expand Down
9 changes: 4 additions & 5 deletions samples/mqtt5/mqtt5_pubsub/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ int main(int argc, char *argv[])
Utils::cmdData cmdData = Utils::parseSampleInputPubSub(argc, argv, &apiHandle, "mqtt5-pubsub");

// 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)
Expand Down Expand Up @@ -107,9 +109,6 @@ int main(int argc, char *argv[])
// Create Mqtt5Client
std::shared_ptr<Aws::Crt::Mqtt5::Mqtt5Client> client = builder->Build();

// Clean up the builder
delete builder;

if (client == nullptr)
{
fprintf(
Expand Down
7 changes: 4 additions & 3 deletions samples/mqtt5/mqtt5_shared_subscription/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ class sample_mqtt5_client
std::shared_ptr<sample_mqtt5_client> result = std::make_shared<sample_mqtt5_client>();
result->name = input_clientName;

Aws::Iot::Mqtt5ClientBuilder *builder = Aws::Iot::Mqtt5ClientBuilder::NewMqtt5ClientBuilderWithMtlsFromPath(
input_endpoint, input_cert.c_str(), input_key.c_str());
auto builder = Aws::Crt::ScopedResource<Aws::Iot::Mqtt5ClientBuilder>(
Aws::Iot::Mqtt5ClientBuilder::NewMqtt5ClientBuilderWithMtlsFromPath(
input_endpoint, input_cert.c_str(), input_key.c_str()),
[](Aws::Iot::Mqtt5ClientBuilder *ptr) { delete ptr; });
if (builder == nullptr)
{
return nullptr;
Expand Down Expand Up @@ -136,7 +138,6 @@ class sample_mqtt5_client
});

result->client = builder->Build();
delete builder;
return result;
}
};
Expand Down
8 changes: 4 additions & 4 deletions samples/shadow/mqtt5_shadow_sync/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@ int main(int argc, char *argv[])
Utils::cmdData cmdData = Utils::parseSampleInputShadow(argc, argv, &apiHandle);

// 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)
{
Expand Down Expand Up @@ -148,7 +149,6 @@ int main(int argc, char *argv[])

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

fprintf(stdout, "Connecting...\n");
Expand Down
2 changes: 1 addition & 1 deletion servicetests/tests/FleetProvisioning/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ std::shared_ptr<Aws::Crt::Mqtt5::Mqtt5Client> createMqtt5Client(const Utils::cmd
[](Aws::Iot::Mqtt5ClientBuilder *ptr) { delete ptr; });

// Check if the builder setup correctly.
if (!builder)
if (builder == nullptr)
{
printf(
"Failed to setup mqtt5 client builder with error code %d: %s", LastError(), ErrorDebugString(LastError()));
Expand Down

0 comments on commit ed8c9ed

Please sign in to comment.