Unexpected Memory Leak in C++ with std::shared_ptr and Custom Deleter
I'm experimenting with I'm sure I'm missing something obvious here, but I'm working with an unexpected memory leak in my C++ application when using `std::shared_ptr` with a custom deleter. I'm working with C++17 and using the Boost library for certain components. The code is supposed to manage a collection of resources, but I keep noticing that the memory is not being freed correctly, especially when objects go out of scope. I have a shared pointer that utilizes a custom deleter to clean up resources, but even after the shared pointer goes out of scope, the memory usage remains abnormally high. Hereโs a simplified version of my code: ```cpp #include <iostream> #include <memory> #include <vector> class Resource { public: Resource() { std::cout << "Resource acquired" << std::endl; } ~Resource() { std::cout << "Resource released" << std::endl; } }; void customDeleter(Resource* res) { std::cout << "Custom deleter called" << std::endl; delete res; } int main() { std::vector<std::shared_ptr<Resource>> resources; for (int i = 0; i < 5; ++i) { resources.emplace_back(std::shared_ptr<Resource>(new Resource(), customDeleter)); } // Intentionally clear the vector to check for memory leaks resources.clear(); return 0; } ``` After running the program, I expect all resources to be released, but when I check the memory profile, it shows that the memory used by `Resource` objects doesnโt drop as expected. I've tried using `std::weak_ptr` to break any circular dependencies, but that hasn't resolved the scenario. Additionally, I've ensured that there are no other references to the `Resource` objects remaining, and I've also experimented with using `std::make_shared` but faced similar issues. The output of my program does indicate the custom deleter is called, yet memory usage remains high. Has anyone experienced a similar question or can provide insights into what might be causing this leak? What could I be missing in the ownership or lifecycle management of these shared pointers? This is part of a larger API I'm building. What am I doing wrong? This issue appeared after updating to Cpp 3.9. Is this even possible?