Garmaine Staff asked 2 years ago

My team is working on an application, where we need to track memory usage, and provide statistics on how much memory areas of the program utilize (e.g. N bytes used by uncontrolled STL containers). I need to find a way to identify memory allocated in 3rd party libs from STL containers.

The application makes use of 3rd party libraries that either we don't have access to the source code, or have been directed not to make changes to the source. Some of these libraries use standard STL containers, like std::vector<int>, but they have used (or appear to use, in the case of the closed libs) the default std::allocator. We are targeting Windows, with future work planned for Mac and Linux platforms, using C++17 as much as possible.

I've overridden the malloc and free functions; overridden new, new[], delete and delete[] operators; and created an STLAllocator class derived from std::allocator that is used as the _Alloc template parameter for our use of STL containers. For the libraries that provide hooks to replace the memory allocators, I have done so. When the STL containers in the remaining 3rd partly libs use the default std::allocator, I can see their new and delete calls come through the new and delete overrides, but these appear no different to tracking than a call to new or delete made from main.

I've read many great descriptions of how to declare and use your own std::allocator class, been reminded of the template parameter equality issue when providing different allocators, and made aware of an upcoming solution using std::experimental::pmr::polymorphic_allocator, but I haven't found a definitive answer to my question. Is there a way to supplant the default std::allocator for 3rd party libs that don't provide a hook to override the default std::allocator used by STL containers?

For anyone interested, here is the link that describes the template parameter equality issue; it's also a good overview of std::allocator in general: https://blog.feabhas.com/2019/03/thanks-for-the-memory-allocator/