This article has been migrated to: http://cpp.winxgui.com/cn:std-new-conflict-with-mfc
Coexistence of std_new and MFC in winx
Xu Shiwei (copyright statement)
2007-3-8
Cause
Winx's autofreealloc has received a wide range of attention, and many readers are inspired by this reaction.
Yesterday, some readers reported that std_new and MFC cannot coexist. I would like to discuss this question here.
Cause
The following debugging code is generated in the source code file:
# Ifdef _ debug
# Define new debug_new
# UNDEF this_file
Static char this_file [] = _ file __;
# Endif
A macro named new is defined here. This makes it impossible to use all non-standard new operators in the MFC program. This is a defect of MFC.
Std_new uses placement new, which is defined in the header file of the C ++ standard library <New> or <new. h>.
Solution
The recommended solution is to delete the following statements in the debugging code of the MFC in the source code file:
# Define new debug_new
However, this brings about another problem. The MFC program cannot detect the memory leakage in the source code file.
It is also very easy to solve this problem. If you want to use new, use debug_new instead. For example:
Type * A = new type (arg1, arg2 );
Change
Type * A = debug_new type (arg1, arg2 );
Personal recommendations
My personal recommendation is not only to use std_new of winx, but to use debug_new in all code, rather than using new directly.