Discussion:
CFileException::GetErrorMessage()
(too old to reply)
Petar Popara
2006-01-10 13:26:57 UTC
Permalink
I'm compiling using VC 7.1 (2003) compiler. If I try to construct CFile
object and pass to the constructor not existing file and set those flags:
CFile::modeRead | CFile::shareDenyNone, in DEBUG build I got an ASSERT
failure.

By searching the Internet I have found this topic, which exactly describes
my problem, but there is no solution??

http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20564822.html
Joseph M. Newcomer
2006-01-10 17:49:23 UTC
Permalink
I decided years ago to stop using the CFile constructor with a filename. Instead, I
create a CFile object without any associated file and then call CFile::Open. I have found
this actually simplifies my code.

When you get an ASSERT failure, it is essential to report the contents of the message box,
such as what file and line it occurred on. This helps us diagnose the problem. Otherwise,
all we can say is "you have a bug".

Given you have a console application, there are many possible failure modes, and without
knowing the exact details (not just some vague handwave about an assertion), it is
impossible to guess what you've done wrong.
joe
Post by Petar Popara
I'm compiling using VC 7.1 (2003) compiler. If I try to construct CFile
CFile::modeRead | CFile::shareDenyNone, in DEBUG build I got an ASSERT
failure.
By searching the Internet I have found this topic, which exactly describes
my problem, but there is no solution??
http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20564822.html
Joseph M. Newcomer [MVP]
email: ***@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
Petar Popara
2006-01-11 08:33:52 UTC
Permalink
Well, I catch CFileException exception as CException inside catch() and when
I do this:

char msg[999];
e.GetErrorMessage(msg, sizeof(msg));

I got this dialog:

---------------------------
Microsoft Visual C++ Debug Library
---------------------------
Debug Assertion Failed!

Program: ...MyApp\Debug\MyApp.exe
File: apphelp.cpp
Line: 30

For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.

(Press Retry to debug the application)
---------------------------
Abort Retry Ignore
---------------------------
Petar Popara
2006-01-11 08:45:38 UTC
Permalink
Well, since the error occur here (apphelp.cpp, line 30):

char szFormat[256];
if (!_AfxLoadString(nIDS, szFormat) != 0)
{
TRACE1("Error: failed to load AfxFormatString string 0x%04x\n", nIDS);
ASSERT(FALSE);

and LoadString() fails, I think I should include in my project some resource
file defining those (MFC) resources (strings). I have found this:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfcnotes_tn023.asp

and I think I need to include AFXRES.RC, but I am surprised it wasn't
already included with the rest of MFC stuff. I hope this will not make my
app much larger?
Joseph M. Newcomer
2006-01-11 19:37:04 UTC
Permalink
You said you had a console app. AppWizard will not magically include things that are not
part of a console app, such as the default MFC strings.
joe
Post by Petar Popara
char szFormat[256];
if (!_AfxLoadString(nIDS, szFormat) != 0)
{
TRACE1("Error: failed to load AfxFormatString string 0x%04x\n", nIDS);
ASSERT(FALSE);
and LoadString() fails, I think I should include in my project some resource
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfcnotes_tn023.asp
and I think I need to include AFXRES.RC, but I am surprised it wasn't
already included with the rest of MFC stuff. I hope this will not make my
app much larger?
Joseph M. Newcomer [MVP]
email: ***@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

Joseph M. Newcomer
2006-01-11 19:36:14 UTC
Permalink
Note that you should always write Unicode-aware

TCHAR msg[MAX_MESSAGE_SIZE]; // do a #define of this value
GetErrorMessage(msg, MAX_MESSAGE_SIZE);

or at least
GetErrorMessage(msg, sizeof(msg)/sizeof(TCHAR));

So, you hit Retry to debug, right? And you saw some code. What did the code say?
ASSERT(FALSE), right? And why did it fail? Because a LoadString had failed. And why did
the LoadString fail? Because the string did not exist! What string ID? Well, since you
are in the debugger, of course you looked immediately at the value of nIDS, the parameter
passed in. What was it? Then, of course, you looked back in the stack to see who called
this. Who? Then, obviously, you looked in your STRINGTABLE to see if that string was
present. Was it?

You have massive amounts of information available to you to help give a coherent bug
report. Without that information, there is little hope we can offer more than "you have
done something wrong. Fix it."
joe
Post by Petar Popara
Well, I catch CFileException exception as CException inside catch() and when
char msg[999];
e.GetErrorMessage(msg, sizeof(msg));
---------------------------
Microsoft Visual C++ Debug Library
---------------------------
Debug Assertion Failed!
Program: ...MyApp\Debug\MyApp.exe
File: apphelp.cpp
Line: 30
For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.
(Press Retry to debug the application)
---------------------------
Abort Retry Ignore
---------------------------
Joseph M. Newcomer [MVP]
email: ***@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
Loading...