0

我正在尝试使用 AWS SDK For C++ 在 EC2 实例(运行 Amazon Linux 2)和 AWS IoT Core 之间建立连接。这是负责建立连接的代码:

#include <aws/crt/Api.h>
#include <aws/iot/MqttClient.h>
#include <iostream>


using namespace Aws::Crt;

/************************ Setup the Lib ****************************/
/*
* Do the global initialization for the API.
* Set the configuration
*/

const String endpoint = "a19cezbd6aruk0-ats.iot.eu-west-1.amazonaws.com";
const String certificatePath = "../SSL/5588197ef3-certificate.pem.crt";
const String keyPath = "../SSL/5588197ef3-private.pem.key";
const String sendTopic = "send";
const String receiveTopic = "get";
const String clientId = "turnstile";
const String caFile = "../SSL/AmazonRootCA1.pem";

ApiHandle apiHandle;

    /********************** Now Setup an Mqtt Client ******************/
    /*
    * You need an event loop group to process IO events.
    * If you only have a few connections, 1 thread is ideal
    */

auto makeConnection ( ) {

    Io::EventLoopGroup eventLoopGroup(1);
    if (!eventLoopGroup) {
        fprintf(
            stderr, "Event Loop Group Creation failed with error %s\n", ErrorDebugString(eventLoopGroup.LastError()));
        exit(-1);
    }

    Aws::Crt::Io::DefaultHostResolver defaultHostResolver(eventLoopGroup, 1, 5);
    Io::ClientBootstrap bootstrap(eventLoopGroup, defaultHostResolver);

    if (!bootstrap) {
        fprintf(stderr, "ClientBootstrap failed with error %s\n", ErrorDebugString(bootstrap.LastError()));
        exit(-1);
    }

    Aws::Iot::MqttClientConnectionConfigBuilder builder;

    builder = Aws::Iot::MqttClientConnectionConfigBuilder(certificatePath.c_str(), keyPath.c_str());
    builder.WithCertificateAuthority(caFile.c_str());
    builder.WithEndpoint(endpoint);

    auto clientConfig = builder.Build();

    if (!clientConfig) {
        fprintf(
            stderr,
            "Client Configuration initialization failed with error %s\n",
            ErrorDebugString(clientConfig.LastError()));
        exit(-1);
    }

    Aws::Iot::MqttClient mqttClient(bootstrap);
    /*
     * Since no exceptions are used, always check the bool operator
     * when an error could have occurred.
     */
    if (!mqttClient) {
        fprintf(stderr, "MQTT Client Creation failed with error %s\n", ErrorDebugString(mqttClient.LastError()));
        exit(-1);
    }

    /*
     * Now create a connection object. Note: This type is move only
     * and its underlying memory is managed by the client.
     */
    auto connection = mqttClient.NewConnection(clientConfig);
    if (!connection) {
        fprintf(stderr, "MQTT Connection Creation failed with error %s\n", ErrorDebugString(mqttClient.LastError()));
        exit(-1);
    }

    return connection;
}

但是,它在尝试运行时返回以下错误,而不是编译(它非常顺利)

[ec2-user@ip-172-31-23-254 build]$ cmake3 -DCMAKE_INSTALL_PREFIX="/home/ec2-user/MakerSpaceTurnstile" -DCMAKE_BUILD_TYPE="Release" ..

-- LibCrypto Include Dir: /home/ec2-user/MakerSpaceTurnstile/include
-- LibCrypto Shared Lib:  /lib64/libcrypto.so
-- LibCrypto Static Lib:  /home/ec2-user/MakerSpaceTurnstile/lib64/libcrypto.a
-- Configuring done
-- Generating done
-- Build files have been written to: /home/ec2-user/MakerSpaceTurnstile/src/build
[ec2-user@ip-172-31-23-254 build]$ sudo cmake3 --build .Scanning dependencies of target MakerSpaceIoT
[ 50%] Building CXX object CMakeFiles/MakerSpaceIoT.dir/main.cpp.o
[100%] Linking CXX executable MakerSpaceIoT
[100%] Built target MakerSpaceIoT
[ec2-user@ip-172-31-23-254 build]$ ./MakerSpaceIoT 
Fatal error condition occurred in /home/ec2-user/MakerSpaceIoT/Code/awssdk/aws-iot-device-sdk-cpp-v2/crt/aws-crt-cpp/crt/aws-c-common/source/allocator.c:115: allocator != ((void *)0)
Exiting Application
################################################################################
Stack trace:
################################################################################
./MakerSpaceIoT(aws_backtrace_print+0x50) [0x5c27f4]
./MakerSpaceIoT(aws_fatal_assert+0x50) [0x5ba010]
./MakerSpaceIoT(aws_mem_acquire+0x68) [0x5b9574]
./MakerSpaceIoT(_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcEN3Aws3Crt12StlAllocatorIcEEEC2EPKcRKS6_+0xb8) [0x438818]
./MakerSpaceIoT() [0x432420]
./MakerSpaceIoT(__libc_csu_init+0x60) [0x5c75c0]
/lib64/libc.so.6(__libc_start_main+0x8c) [0xffff92728c8c]
./MakerSpaceIoT() [0x432a30]
Aborted

这里可能出了什么问题?IoT Core 的政策似乎允许主题和客户端 ID。

4

1 回答 1

0

事实证明这是因为处理程序超出了函数的范围。

于 2021-05-27T19:37:38.680 回答