1

I'm trying to replace standard C functions with my own implementation.

void* malloc(size_t size) {}
void free(void*) {}

gives me the following warnings/errors:

1>main.cpp(26): warning C4273: 'malloc': inconsistent dll linkage
1>  C:\Program Files (x86)\Windows Kits\10\Include\10.0.10240.0\ucrt\corecrt_malloc.h(97): note: see previous definition of 'malloc'
1>main.cpp(31): warning C4273: 'free': inconsistent dll linkage
1>  C:\Program Files (x86)\Windows Kits\10\Include\10.0.10240.0\ucrt\corecrt_malloc.h(85): note: see previous definition of 'free'
1>ucrtd.lib(ucrtbased.dll) : error LNK2005: _free already defined in main.obj
1>ucrtd.lib(ucrtbased.dll) : error LNK2005: _malloc already defined in main.obj
1>     Creating library ..\bin\\app.lib and object ..\bin\\app.exp
1>..\bin\\app.exe : fatal error LNK1169: one or more multiply defined symbols found

Is there a way to replace malloc/free function in Visual C++ 2015 without using #define? (I use third party libs and do not want to modify theirs code).

Alexander Dyagilev
  • 1,139
  • 1
  • 15
  • 43
  • You might try: `void* __cdecl malloc`. Same for `free`. – CristiFati Apr 21 '17 at 15:22
  • If this is for debugging purposes, then you might consider using the CRT heap debug API. https://msdn.microsoft.com/en-us/library/974tc9t1.aspx#BKMK_Track_Heap_Allocation_Requests – Ben Apr 21 '17 at 15:27
  • See also https://msdn.microsoft.com/en-us/library/820k4tb8.aspx Pretty much every debugging feature you might want is available in the CRT debug heap. – Ben Apr 21 '17 at 15:29
  • 1
    If you want to find memory leaks, I recommend Visual Leak Detector. – sergiol Apr 21 '17 at 17:33
  • `__cdecl` does nothing in standard config... OK, I've checked - did not help. – Alexander Dyagilev Apr 21 '17 at 17:57
  • Ben, thanks, I'll check this later. – Alexander Dyagilev Apr 21 '17 at 17:57
  • No, I do not want to find memory leaks. I've got a code that damages heap... AppVerifier, DrMemory - does not work for me. – Alexander Dyagilev Apr 21 '17 at 17:58
  • keeping to the original question, after the statement: `#include ` insert the two statements: `#undefine malloc` and `#undefine free` – user3629249 Apr 23 '17 at 19:21
  • user3629249, what's the point? Got exactly the same warnings and errors. – Alexander Dyagilev Apr 24 '17 at 17:07
  • Ben, unfortunately this does not suit my needs... – Alexander Dyagilev Apr 24 '17 at 17:08
  • @AlexanderDyagilev Why does Ben's link not suit your needs? What exactly do you want to do in your own malloc/free that you can't do in the hook? – Werner Henze Apr 25 '17 at 07:04
  • This sounds like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). *Why* do you want to replace `malloc` and `free`? What are you *really* trying to accomplish? – dbush Apr 25 '17 at 12:35
  • I'm trying to implement the technique described here: http://stackoverflow.com/questions/2560858/modern-equivalent-of-boundschecker-for-visual-studio-2008/2561199#2561199 – Alexander Dyagilev Apr 25 '17 at 18:06
  • 1
    Have you seen this SO question? [Problem in overriding malloc](http://stackoverflow.com/questions/1094532/problem-in-overriding-malloc) – Phil Brubaker Apr 26 '17 at 11:47
  • Are you including stdlib.h in the main.cpp source? If not, you should not be seeing linking error at all. If possible, please post the complete source. – vivekn Apr 27 '17 at 09:07

3 Answers3

3

Yes, this is possible. The way this will work is that your code must hook malloc() and free() in msvcrt.dll before any code has an opportunity to call these functions. I believe you should be able to use Microsoft Detours to hook these functions. Also you might want to hook HeapAlloc() and HeapFree() in kernel32.dll instead since they will eventually call this API.

Man Vs Code
  • 1,058
  • 10
  • 14
1

Assuming you want to debug memory issue in a third party library, providing your own implementation of free() and malloc() and linking it with your third party library will not work.

As the linker reports in the error message, you have two implementations of the same function. One in your files (app.lib), and another one in ucrtd.dll which is called the Common Runtime Library and which is where malloc() and free() are provided.

If you remove ucrtd.dll from your project, you will remove as well many other functions that are needed by your application. So this is probably not an option.

As Ben mentionned, if it is for debugging memory issues, you can use the CRT Debug Heap API, which are built-in Tools that you activate by defining some symbols (at compile time) and some additional functions (see the documentation). Nonetheless you can hook the memory allocation process, but without going that far, you can display and check many things at first.

This is of course more or less easy to do depending on whether you can access the source code of the library you want to debug, which we do not know. If you do not have access to the source code, other tools might be needed.

Update: Depending on whether you can access the source of the Library you want to debug, you might be interessted by one the three methods cited in this question: Wrapping of function in the same file

Community
  • 1
  • 1
Heyji
  • 1,113
  • 8
  • 26
  • I'm trying to implement the technique described here: http://stackoverflow.com/questions/2560858/modern-equivalent-of-boundschecker-for-visual-studio-2008/2561199#2561199. All this CRT Debug Heap API is completely useless for me. – Alexander Dyagilev Apr 25 '17 at 18:14
  • Then you should update your question with this information, plus explain why the CRT Debug Heap API is not suitable to debug a "code that dammages heap" in your case. – Heyji Apr 26 '17 at 09:55
0

You can not replace these functions even you setup /FORCE:MULTIPLE to linker because it will use first found object. And it will be implementation of CRT. Do you really want to substitute this function? If answer is 'yes', you shouldn't use any C Run-Time Library functions at all - and there will be more pain (sprintf(), strlen() and other string function, fopen() also uses CRT). Also you should additionally setup you project to give up CRT. Think again.

Prime Ape
  • 130
  • 3
  • 12