Overloading malloc to track memory usage of third party libraries [duplicate]
Possible Duplicate:
Best strategy for profiling memory usage of my code (open source) and 3rd party code(closed source)
I am thinking of implementing a memory tracking tool to track malloc's in my code by having a library that I will link in at compile time to override malloc with a macro to add some additional debugging info that will print some stuff to a log. Is it possible to possibly do this with a third party library that I do not have source code, or possibly debug libraries available? Can you lin开发者_如何学Pythonk in a library like I am talking about to a third party library and it will use the methods (or macros) defined in my library?
Thanks
I don't think you can do that, malloc is a system call.
The binary libraries aren't actually calling malloc(), they're loading a library stored on your machine into memory. Then executing the function at the correct memory address.
Redefining malloc() wont do anything except confuse things.
Use profiler: AQTime is a really good one for Visual Studio.
It depends on what platform / OS you are trying to do that on:
- If you are using a RTOS on embedded device, you may be able to change the code directly in there too...
- If you are on Linux, you may be able to find the code somewhere and change it there to recompile, but you may have a pretty serious performance impact on your platform.
- For others, you can use a wrapper in your application, call it "os_malloc" or something like that and implement that function for any debugging or anything like that.
The last one is probably your safest bet no matter what...
精彩评论