-
October 30th, 2000, 11:37 AM
#1
App Wizard Genereated Code
App Wizard will generate the following code in .cpp files.
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
Does anyone out there know what this code does? MSDN Library states that it is used by ASSERT and VERIFY macros yet if you comment out the code above, ASSERT and VERIFY macros still seem to work as usual. So what do these lines of code accomplish?
-
October 30th, 2000, 12:06 PM
#2
Re: App Wizard Genereated Code
The preprocessor #define isn't limited to the scope of the file. If you #define THIS_FILE as "MyClass.cpp" in one file, then in the next file (say "MyDialog.cpp"), you have to #undef THIS_FILE and then redefine it to "MyDialog.cpp".
In other words, if you delete that block, then THIS_FILE will be pointing to the wrong filename.
O-Deka-K
-
October 30th, 2000, 12:09 PM
#3
Re: App Wizard Genereated Code
Oh wait, but it uses a static variable instead of a #define after it undefines it. Ack, I don't know...
O-Deka-K
-
October 30th, 2000, 12:15 PM
#4
Re: App Wizard Genereated Code
Thanks for the reply... but why is that code generated in the first place? Take that code out or comment out that code and everything seems to run normally. So why is it generated in the first place?
-
October 30th, 2000, 12:25 PM
#5
Re: App Wizard Genereated Code
Looking at the source code for MFC, it seems that ASSERT uses THIS_FILE to find the line number that the assertation file occured on. So, if you don't use ASSERT in your code anywhere, or if you never get an assertation failure in your source, then it won't matter. It also seems to use it in the new operator for the same thing. Again, if you don't get any new errors (memory leaks, I guess), then you won't see any difference.
BTW, MFC defines THIS_FILE in a whole lot of files, so even if you remove it from all of your own source files, it will still be defined as something.
O-Deka-K
-
October 30th, 2000, 12:52 PM
#6
Re: App Wizard Genereated Code
ASSERT and VERIFY MACROS call the MFC helper function AfxAssertFailedLine() which is defined like this:
BOOL AFXAPI AfxAssertFailedLine(LPCSTR lpszFileName, int nLine);
AfxAssertFailedLine() is the function that pops up a message box and tells you the source file (lpszFileName) and line number (nLine) where the ASSERT failed.
ASSERT(exp) expands approximately to:
If(!exp)
AfxAssertFailedLine(THIS_FILE,__LINE__)
THIS_FILE is initially defined in Afx.h as __FILE__ which is an ANSI C MACRO that expands to a constant string which is the name of the current source file.
Assume that your source file has a very long path name (i.e. “c:\<real long path>\source.cppâ€). This means that all of the ASSERT and VERIFY and MACROS will expand like this:
If(!exp)
AfxAssertFailedLine(“c:\<real long path>\source.cppâ€,line);
If you use a lot of ASSERT’S and VERIFY’S then the string literal “c:\<real long path>\source.cpp†will be duplicated for each one resulting in a lot of excess in your debug executable.
The App-Wizard solves this problem by UNDEFINING THIS_FILE, then RE-DEFINING it as a static character array like this:
#undef THIS_FILE
static char [] THIS_FILE __FILE__
so that now your ASSERT and VERIFY MACROS expand like this:
if(!exp)
AfxAssertFailedLine(THIS_FILE,__LINE__);
Where THIS_FILE is now only a pointer to an array (4 bytes) rather than an entire string literal. The net result is a smaller executable. The reason it is done only in the debug mode is because AfxAssertFailedLine() is never called in the release mode, so THIS_FILE isn’t needed.
mnd
-
November 9th, 2010, 12:01 PM
#7
Re: App Wizard Genereated Code
Hi. Is it any way how to call callback from ASSERT macro or better from within AfxAssertFailedLine() function? I would like to log callstack, file, line, time and etc. info to special log file for testing purposes. Is it something like AfxAddAssertCallback()?
Thank you very much.
B.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|