views:

143

answers:

5

Hi,

I'd like the following to appear in every source file in my Visual C++ 2005 solution:

  #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
  #define new DEBUG_NEW

Is there a way of doing this without manually copying it in? Compiler option?

Cheers

+1  A: 

You could insert that #define into stdafx.h or common.h or any other header file that gets included into each source file.

sharptooth
+1  A: 

Compiler option?

Yes, you can customize a list of defines in the project properties (either under “Preprocessor” or “Advanced,” as far as I remember). These defines will be present in each source file.

Konrad Rudolph
+5  A: 

The command line option /D can be used to define preprocessor symbols. I don't know, though, whether it can also be used to define macros with arguments, but it should be an easy matter to test that.

Edit: Failing that, the /FI option ("force include") should allow you to do what you want. Quoting the MSDN documentation:

This option has the same effect as specifying the file with double quotation marks in an #include directive on the first line of every source file [...] .

You can then put your #defines in that forced include file.

Martin B
Unfortunately both of these suggestions result in lots of errors.I'm trying to track down memory leaks using the suggestion here:http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/5a98b72e-c927-4f4b-9441-8a73575dfb10
Steven
What error messages, exactly? Are you defining _CRTDBG_MAP_ALLOC?If that's not the problem, you may be running into the problem described here:http://stackoverflow.com/questions/619467/macro-to-replace-c-operator-new
Martin B
This approach will not work, if file includes header, in which placementnew is used. Reason is that DEBUG_NEW macro is incompatible with placement new, and that file is treated as included at first line. In practice problem will be with STL and Boost headers.
Konstantin
+4  A: 

I'd advise against using this #define. Re-defining new is not portable and if you do it in this way then you prevent anything subsequently using a placement new from working. If you 'force' this #define before a file's manually #includes take effect then you risk incompatibilities between library header files and their source files and you will get 'surprise' errors in library files that use placement new (frequently template/container classes).

If you are going to redefine new, then make it explicit and leave it in the source.

Charles Bailey
A: 

You could put the #defines into an h file, but without putting the #ifndef guard in the h file. Then #include the file in each of your source files.

I am not endorsing redefining new, BTW.

JXG