diff --git a/samples/fleet_provisioning/mqtt5_fleet_provisioning/main.cpp b/samples/fleet_provisioning/mqtt5_fleet_provisioning/main.cpp index 6c9d0f15a..4c69ff1de 100644 --- a/samples/fleet_provisioning/mqtt5_fleet_provisioning/main.cpp +++ b/samples/fleet_provisioning/mqtt5_fleet_provisioning/main.cpp @@ -82,11 +82,13 @@ struct RegisterThingContext std::shared_ptr 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::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())); @@ -128,7 +130,6 @@ std::shared_ptr createMqtt5Client(const Utils::cmd // Create Mqtt5Client std::shared_ptr client = builder->Build(); - delete builder; return client; } diff --git a/servicetests/tests/FleetProvisioning/main.cpp b/servicetests/tests/FleetProvisioning/main.cpp index 80fbb8712..64a648006 100644 --- a/servicetests/tests/FleetProvisioning/main.cpp +++ b/servicetests/tests/FleetProvisioning/main.cpp @@ -84,11 +84,14 @@ struct RegisterThingContext std::shared_ptr 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::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())); @@ -130,7 +133,6 @@ std::shared_ptr createMqtt5Client(const Utils::cmd // Create Mqtt5Client auto client = builder->Build(); - delete builder; fprintf(stdout, "Connecting...\n"); if (!client->Start())