Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore globally overridden new/delete

Hi I'm using a library that has globally overridden new/delete. But I have a problem with this library, the problem is that it has to be manually initialized in the main function.

Now I'm trying to use another library that initializes a few functions before main is called, unfortunately this library uses new within these functions. So I get errors because the memory manager that uses the overridden new/delete keywords are not initialized yet.

I'd really like to use the default memory manager because I want to add unit testing to this library. It would not make much sense to use the memory used my the library I want to test also used by my Unit Testing library.

So my question is if it's possible to ignore global overridden new/delete when including the second library and just use the default new/delete?

I'm using visual studio 2010 on Windows 7 with the standard C++ compiler.

like image 324
NomenNescio Avatar asked Sep 21 '12 12:09

NomenNescio


1 Answers

I do not think this is possible without modifying the library itself. I guess it is about a static library (inside a dll, the overridden new/delete will be pointed by the functions inside the dll.)

You can remove an obj file from a static library by using the command (Visual command prompt):

  LIB /REMOVE:obj_to_remove /OUT:removed.lib input.lib

To find out what obj to remove, first run:

  DUMPBIN /ARCHIVEMEMBERS input.lib

You will see lines such as

    Archive member name at 14286: /0  compilation.dir\objfile1.obj

14286 'identifies' the obj file. To see where each symbol is, run:

 DUMPBIN /LINKERMEMBER:1 input.lib > members.txt

and look for the new/delete. members.txt will contains the mangled names of each symbols and an identifier of the obj in which this symbol is. For instance

    14286 ?_Rank@?$_Arithmetic_traits@C@std@@2HB

The 14286 is telling you the obj 'identifier' in which the symbol lies. If you have trouble finding new/delete, you can run:

 DUMPBIN /SYMBOLS input.lib > sym.txt

which will flush into sym.txt the mangled and unmangled names for each symbol.

At the end, remove the obj file with the LIB command above by replacing obj_to_remove by compilation.dir\objfile1.obj in our example, and link against removed.lib.

Now, if you are not lucky, other symbols you need may be in the same object file as the new/delete. In that case, you can "hack" the lib using something like this (say renaming new to dew and delete to nelete.)

like image 87
Raffi Avatar answered Oct 03 '22 01:10

Raffi