tags:

views:

61

answers:

2

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 check b) Without the sources for the DLL?

A: 

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.

Elemental
+1  A: 

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.

stijn
I think this does not exactly, what i wont to do: I want to call code in the application FROM the DLL. Export does the opposite thing, but that brings different problems. I'll have the overloaded operators in the Dll. Is it in general possible to call code in the host from the Dll without calling functions to register it etc?
RED SOFT ADAIR
sorry but I fail to understand what you mean exactly.. can you provide some code example maybe? also can you rephrase 'I want to call code': do you mean 'main program wants to call functions'? or do you rather mean something like 'dll wnats to access data (objects) allocated in the main program'?
stijn
The DLL should have some 'Imports', that resolve to code in the host application - so the dll should call functions in the host application. That does not only belong to operators new etc. I actually don't know if thats possible. Doesnt seem like.
RED SOFT ADAIR
even if it is possible, sounds like it is going to be a pain to work with, especially for the new/delete operators. Why do you want to do it that way, and not the 'normal' way of host application calling functions in the dll? see edit for possible solution though.
stijn