From ed8c9ed75950edb2bea79d448d6c2ee3db6a86df Mon Sep 17 00:00:00 2001 From: Igor Abdrakhimov Date: Wed, 11 Sep 2024 10:04:43 -0700 Subject: [PATCH] Use ScopedResource instead of raw pointer in all samples --- .../mqtt5_basic_report/main.cpp | 19 ++++++++++++++----- .../mqtt5_fleet_provisioning/main.cpp | 2 +- samples/jobs/mqtt5_job_execution/main.cpp | 7 ++++--- samples/mqtt5/mqtt5_pubsub/main.cpp | 9 ++++----- .../mqtt5/mqtt5_shared_subscription/main.cpp | 7 ++++--- samples/shadow/mqtt5_shadow_sync/main.cpp | 8 ++++---- servicetests/tests/FleetProvisioning/main.cpp | 2 +- 7 files changed, 32 insertions(+), 22 deletions(-) diff --git a/samples/device_defender/mqtt5_basic_report/main.cpp b/samples/device_defender/mqtt5_basic_report/main.cpp index 61f2bf522..ce36b7402 100644 --- a/samples/device_defender/mqtt5_basic_report/main.cpp +++ b/samples/device_defender/mqtt5_basic_report/main.cpp @@ -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::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()); @@ -119,9 +131,6 @@ int main(int argc, char *argv[]) // Create Mqtt5Client std::shared_ptr client = clientConfigBuilder->Build(); - // Clean up the builder - delete clientConfigBuilder; - if (client == nullptr) { fprintf( diff --git a/samples/fleet_provisioning/mqtt5_fleet_provisioning/main.cpp b/samples/fleet_provisioning/mqtt5_fleet_provisioning/main.cpp index 4c69ff1de..eb2b71244 100644 --- a/samples/fleet_provisioning/mqtt5_fleet_provisioning/main.cpp +++ b/samples/fleet_provisioning/mqtt5_fleet_provisioning/main.cpp @@ -88,7 +88,7 @@ std::shared_ptr 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())); diff --git a/samples/jobs/mqtt5_job_execution/main.cpp b/samples/jobs/mqtt5_job_execution/main.cpp index 65e91d520..5efb68762 100644 --- a/samples/jobs/mqtt5_job_execution/main.cpp +++ b/samples/jobs/mqtt5_job_execution/main.cpp @@ -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::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) @@ -101,7 +103,6 @@ int main(int argc, char *argv[]) // Create Mqtt5Client std::shared_ptr client = builder->Build(); - delete builder; /************************ Run the sample ****************************/ fprintf(stdout, "Connecting...\n"); diff --git a/samples/mqtt5/mqtt5_pubsub/main.cpp b/samples/mqtt5/mqtt5_pubsub/main.cpp index 9f65cae4e..72f6750d1 100644 --- a/samples/mqtt5/mqtt5_pubsub/main.cpp +++ b/samples/mqtt5/mqtt5_pubsub/main.cpp @@ -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::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) @@ -107,9 +109,6 @@ int main(int argc, char *argv[]) // Create Mqtt5Client std::shared_ptr client = builder->Build(); - // Clean up the builder - delete builder; - if (client == nullptr) { fprintf( diff --git a/samples/mqtt5/mqtt5_shared_subscription/main.cpp b/samples/mqtt5/mqtt5_shared_subscription/main.cpp index 998792ae9..80ea5d2ae 100644 --- a/samples/mqtt5/mqtt5_shared_subscription/main.cpp +++ b/samples/mqtt5/mqtt5_shared_subscription/main.cpp @@ -45,8 +45,10 @@ class sample_mqtt5_client std::shared_ptr result = std::make_shared(); 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::NewMqtt5ClientBuilderWithMtlsFromPath( + input_endpoint, input_cert.c_str(), input_key.c_str()), + [](Aws::Iot::Mqtt5ClientBuilder *ptr) { delete ptr; }); if (builder == nullptr) { return nullptr; @@ -136,7 +138,6 @@ class sample_mqtt5_client }); result->client = builder->Build(); - delete builder; return result; } }; diff --git a/samples/shadow/mqtt5_shadow_sync/main.cpp b/samples/shadow/mqtt5_shadow_sync/main.cpp index 12bc00353..278ad9c3a 100644 --- a/samples/shadow/mqtt5_shadow_sync/main.cpp +++ b/samples/shadow/mqtt5_shadow_sync/main.cpp @@ -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::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) { @@ -148,7 +149,6 @@ int main(int argc, char *argv[]) // Create Mqtt5Client std::shared_ptr client = builder->Build(); - delete builder; /************************ Run the sample ****************************/ fprintf(stdout, "Connecting...\n"); diff --git a/servicetests/tests/FleetProvisioning/main.cpp b/servicetests/tests/FleetProvisioning/main.cpp index 64a648006..736f2938d 100644 --- a/servicetests/tests/FleetProvisioning/main.cpp +++ b/servicetests/tests/FleetProvisioning/main.cpp @@ -91,7 +91,7 @@ std::shared_ptr 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()));