How to make my dependencies call my global operator new?

I have a test application that is associated with some DLL (or .so). In my main application, I defined a global new / delete like this:

void* operator new(size_t n) { .... } void operator delete(void* p) { ... } 

But I noticed that statements are only called for the things that I highlight in my main application, but not if one of the DLLs does.

How to make a selection in a DLL through my new / delete statement? (This should also include the memory allocated by STL, so if one of the DLLs has std :: string, I would like my new operator to be called when the STL allocates its internal std :: string buffer).

I'm more interested in the Windows solution, but Linux will also be appreciated.

edit: maybe I was not clear at first, this test application that I did was designed to track memory usage for several automatically generated classes defined in a DLL. Creating my own distributor and using it in the generated STL code structures is not an option, all the more there are other non-STL distributions. But, seeing the answers, I think the best option is either to use the profiler, or just control the memory usage with perfmon.

+4
source share
3 answers

I would like my new operator to be called when STL allocates its internal std :: string internal buffer

typedef std::basic_string<char, std::char_traits<char>, ALLOCATOR> mystring;

The code in DLLs already uses their own implementation of new , and there is no good reason why defining a custom implementation should magically change the implementation that DLLs use (what if they use their own implementation?).

So, if you want the strings to use your allocator, you need to explicitly create them as such.

+2
source

Everything that is intended to use your global definitions must be compiled with these definitions. The method you use will not override everything that is already compiled in the DLL or even in other source files that do not include these definitions. In many cases, dispensers and standard functions will also not use these functions, even if they are visible.

If you really need to do this, you will have to intercept calls in malloc (and another distribution procedure). It is not simple. You cannot just do this from code. How to do this, you have to tell the linker. On Linux, I think this is LD_PRELOAD, although I don’t remember, and on Windows I’m not at all sure.

If you can indicate why you want to do this, I can offer an alternative solution.

+1
source

You cannot completely do what you want to do. Too many corner cases when a memory leak occurs.

The closest I think you can do by doing the following: Each class in your dll / .so will have to have a static factory / destroy method. Pass a pointer to the distribution function of the factory function and the release function for the destroy method in each class in each dll / .so.

An example of how to approach is google for the HORDE memory allocation library that is approaching.

Another thing you need to pay attention to is the various C ++ class plugin libraries that let you load any DLL / .so as a plugin. Last time I checked, there were at least 10 such libraries with a source in googlesphere. :)

+1
source

All Articles