开发者

How do i make a C++ DLL call my overloaded global new operator?

I have overloaded global new/delete (and new[] / delete[]) to fill and check guarded blocks. Works fine. Now i link to C++ DLLs passing STL-Container instances that are filled or modified by the DLLs. When destructing these containers i run into an error, as they were not allocated using my overloaded new operator and vice versa the dlls generate errors when freeing container elements that were created using my overloaded new.

How can i make DLLs call my new operator? For some DLLs i have the sources, for others i don't have it.

There must be a overall approach as i.e. the Visual Studio Runtime DLLs MSVCP*.DLL call my overloaded operators. How can i make other DLLs call my operators too?

a) With having the sources for the DLL? and che开发者_如何学运维ck b) Without the sources for the DLL?


for dlls you can compile, you can get them to call your overloaded methods by

  1. making sure the calling code includes the header defining your overloads
  2. exporting those overloads from your dll by specifying them in an export file

here are the exports (using mangled names, never found another way to do it) for new/delete/new[]/delete[], throwing versions.

x86:

EXPORTS
  ??2@YAPAXI@Z
  ??3@YAXPAX@Z
  ??_U@YAPAXI@Z
  ??_V@YAXPAX@Z

x64:

EXPORTS
  ??2@YAPEAX_K@Z
  ??3@YAXPEAX@Z
  ??_U@YAPEAX_K@Z
  ??_V@YAXPEAX@Z

I don't think this works for dlls that you're not compiling yourself though (at the time they were built the linker already took care of finding the references to the methods); to do that you might have to use rather dirty tricks like hooking the crt for your process.

edit for the other way around, you could pass allocators from the host application into the dll and make sure the dll only uses those allocators to allocate, instead of new/delete. Havee thos alloactors call your overloaded new/delete in turn. It's a bit messy but should work, also with STL since you can specify the allocator for those containers; but again, this does not solve anything if you want a dll for which you do not have the code to allocate using your bounds checking code.


I think you need to place the new/delete code in a DLL and ensure that both the exe and your extra DLL's call this common code.

Even this approach has problems so for me it is better to ensure architecturally that the module that allocated a chunk of memory is the same module that deletes it however this isn't a simple requirement is many cases.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜