1

I am designing a library where I need AWS SDK for S3 and Kinesis as wrapped sub-components. In an executable, Aws::InitAPI() and Aws::ShutdownAPI() can be called once, so I am trying to design a wrapper which can call Init and Shutdown only one time for any number of components.

Consider the following example. Only when declared as a global shared_ptr, when the shutdown API is called, the application crashes.

To recreate the issue, you can uncomment the start 4, compile, and run.

#include <thread>
#include <aws/core/Aws.h>
namespace axon {
 class AwsStack {
 Aws::SDKOptions options;
 public:
 AwsStack() {
 options.loggingOptions.logLevel = Aws::Utils::Logging::LogLevel::Trace;
 printf("Aws::SDK::Init()\n");
 Aws::InitAPI(options);
 }
 ~AwsStack() {
 printf("Aws::SDK::Shutdown()\n");
 Aws::ShutdownAPI(options);
 }
 };
};
std::shared_ptr<axon::AwsStack> awsInstance1;
int main(int argc, char** argv)
{
 // // start 1
 // axon::AwsStack awsInstance0; // this does not crash
 // // end
 // // start 2
 // std::shared_ptr<axon::AwsStack> awsInstance2 = std::make_shared<axon::AwsStack>(); // this does not crash
 // // end
 // // start
 // awsInstance1 = std::make_shared<axon::AwsStack>(); // this does not crash
 // awsInstance1.reset();
 // // end
 
 // // start
 // awsInstance1 = std::make_shared<axon::AwsStack>(); // but this will crash
 // // end
 return 0;
}

Compiled with following:

c++ -ggdb -o example example.cpp -laws-cpp-sdk-core

Any thoughts about why it crashes? How can I fix it?

Remy Lebeau
610k36 gold badges516 silver badges875 bronze badges
asked Sep 23, 2025 at 11:53
3
  • 4
    Two design flaws : Global variables, and using shared_ptr where it may not even be needed. Make sure you control the lifetime of your objects from main Commented Sep 23, 2025 at 12:19
  • Where is crash report? What kind of crash is reported and where crashing thread callstack is (where it points to your code)? Commented Sep 23, 2025 at 12:44
  • how I can fix it? Get rid of the global shared_ptr. Use the first implementation of a local automatic variable in main, that does not crash. Commented Sep 23, 2025 at 13:21

1 Answer 1

4

This is some kind of initialization order fiasco. The case 4 is specifically mention in the manual Initializing and shutting down the AWS SDK for C++. Just don't do that.

The SDK for C++ and its dependencies use C++ static objects, and the order of static object destruction is not determined by the C++ standard. To avoid memory issues caused by the nondeterministic order of static variable destruction, do not wrap the calls to Aws::InitAPI and Aws::ShutdownAPI into another static object.

Possible dup: Destruction order of static objects in C++

answered Sep 23, 2025 at 12:02
Sign up to request clarification or add additional context in comments.

4 Comments

Looking at your link I would say it may well just be missing the InitApi call (and the ShutdownApi) call), which then when any call to Aws is made just doesn't have the proper stuff initialized. So could be something even more simpel than a SIOF issue, just a "did you read the manual and do you understand C++ object lifecycle" issue. (Disclaimer I did not use AWS ever, but documentation seems to be very clear ;) )
a little bit of empathy goes a long way, these platforms thrive on stupid and dumb people like me.
If you think I lack empathy, then I am sorry, that's not my intention. For me my comments are just factual hoping that they might help you or others.
thanks, I was actually aware there where is an inherent complexity comes with libraries where the underlying libs have data sync and threads to manage. I was trying to figure a way to wrap aws lib inside the wrapper I am writing which inturn when used can automatically init aws-sdk without the user needing to do it explicitly. anyways, thanks for confirming that I need to do this the hard way :'(

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.