3

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?

Brian Tompsett - 汤莱恩
  • 5,438
  • 68
  • 55
  • 126
Steven Keith
  • 1,759
  • 14
  • 22

6 Answers6

5

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
  • 22,970
  • 6
  • 51
  • 71
  • 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 Keith Aug 25 '09 at 08:42
  • 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 Aug 25 '09 at 09:07
  • 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 Tenzin Sep 14 '09 at 11:22
4

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.

CB Bailey
  • 700,257
  • 99
  • 619
  • 646
2

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

sharptooth
  • 163,328
  • 92
  • 501
  • 942
1

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
  • 506,650
  • 124
  • 909
  • 1,183
1

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
  • 7,093
  • 7
  • 30
  • 61
1

You can just define your own global new operator somewhere in your code and compile it conditionally. Do not forget to include all 4 variations of new( plain and array one with and without nothrow) and two variations of delete(plain and array one). There is a whole chapter on the matter in my copy of Effective C++, Third Edition (Chapter 8)

#ifdef MYDEBUG
void* operator new(std::size_t size) { <your code here> }
void operator delete(void* p) { <your code here> }
#endif
uuu777
  • 669
  • 4
  • 20