awslabs / amazon-kinesis-video-streams-producer-sdk-cpp

Amazon Kinesis Video Streams Producer SDK for C++ is for developers to install and customize for their connected camera and other devices to securely stream video, audio, and time-encoded data to Kinesis Video Streams.
Apache License 2.0
376 stars 337 forks source link

Fix memory leak in failed DefaultCallbackProvider constructor #1200

Open grantralston opened 1 month ago

grantralston commented 1 month ago

Description of changes:

I noticed a leak in memory in our application and was able to trace it to the KVS C++ Producer Library. This situation was mainly occuring when the producer could not reach the internet.

Our application is long-living, and will restart the pipeline with kvssink if it fails very quickly, so in times of internet outage we cycle through instances of kvssink fairly quickly.

img

As is visible in the graph, at the time this dump was taken, KVS Producer was responsible for 30GB of memory coming from createCurlApiCallbacks. On investigation, I was able trace this back to the DefaultCallbackProvider not freeing memory allocated to PClientCallbacks instance when the constructor failed.

This issue was fixed by transitioning the PClientCallbacks instance to a separate RAII instance that will automatically free when the DefaultCallbackProvider instance goes out of scope.

This change has been running in a test environment with the triggering conditions for 4 days and we have seen the leak resolved.

Screenshot 2024-08-05 at 5 25 59 PM

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.