Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on a legacy C++ project and saw these statement on top of a header file:

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


I know it is related with debugging memory leaks stuff, but not sure how it works, or potentially it can be used for other purpose too.

Post here to ask C++ gurus to get more insights.

thanks a million!

What I have tried:

play around with this code snippet and did not find much details on how it is used in this project.
Posted
Updated 8-Mar-22 23:58pm

If you right click on it in Visual Studio you can go to the definition. It is a wrapper for the new operator, and captures some extra debug information every time new is called in the application.
 
Share this answer
 
Comments
Southmountain 17-Dec-20 12:48pm    
if I put cursor on DEBUG_NEW, a tooltip prompts: #define DEBUG_NEW new(this_file, _LINE_),
so all keyword new will be replaced by new(this_file, _LINE_)?
Richard MacCutchan 17-Dec-20 13:46pm    
Yes. It captures extra information to help debug problems with memory allocations.
As external link to the very macro Wikipedia page[^] you may find A Cross-Platform Memory Leak Detector[^] that may give you some insight.
 
Share this answer
 
v2
Comments
Southmountain 17-Dec-20 12:29pm    
thanks for these good links!
CPallini 17-Dec-20 13:31pm    
You are welcome.
For a little bit more info check this out : Memory Allocation Tracking for C++ Code[^] (Shameless plug).

The macros shown there are slightly revised from the default implementation of the DEBUG_NEW macro. The THIS_FILE declarations are no longer used by MFC, AFAIK.
 
Share this answer
 
Comments
Southmountain 17-Dec-20 12:55pm    
thank you for this post! my 5!
To make a long story short, when your output window prints: Detected memory leaks, double clicking that error will take you to the line where you used the "new" operator in your source code. Hence all that __FILE__ and __LINE__ business
 
Share this answer
 
Comments
Southmountain 18-Dec-20 11:44am    
this is the trick that I will master. thank you very much!
NOTE: As of C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.29.30133\atlmfc\include

"THIS_FILE" is still used by MFC...
 
Share this answer
 
Comments
CHill60 9-Mar-22 6:27am    
This should have been posted as a comment to Solution 3. It is not, in itself, a solution to the question posed

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900