I have a very irritating issue while running a C++ application. I am using the pgcpp compiler on the Interix subsystem of Windows Xp. My problem is essentially described here:
I have a class definition in a header file. This header file is included in one source file. This class has two constructors and is basically used to implement a logger. The first constructor takes ostream *out as an argument, while the second overloaded constructor takes a filename and a default boolean value of false. The objective of this second constructor is to get a stream for the filename that we are passing and to start logging messages to it. The code in the constructors is as follows:
MessageLogger::MessageLogger(std::ostream *out): p_out (out), p_ofstream (0)
{
if (p_out)
{
(*p_out) << "Started logging messages" << endl;
}
}
MessageLogger::MessageLogger (char const *filename, bool append_to_file) : p_out (0), p_ofstream (0)
{
if (append_to_file)
{
p_ofstream = new std::ofstream (filename, ios::app);
}
else
{
p_ofstream = new std::ofstream (filename);
}
p_out = p_ofstream;
if (p_out)
{
(*p_out) << "Started logging messages" << endl;
}
}
Where the declarations of p_out and p_ofstream are as follows:
std::ostream *p_out;
std::ofstream *p_ofstream;
unsigned int p_indent_level;
All the three mentioned above are private members. The instantiation of the MessageLogger class is done as:
MessageLogger logger ("filename");
Please note that append_to_file has a default value of false. PGDBG
is also misbehaving. I am inexplicably able to step in when the control is at p_ofstream = new std::ofstream (filename);
and it steps into a random location and then the application crashes.
Also, when I try to see either of Mixed or Disassembly code in PGDBG, the debugger crashes with the message:
jpgdbg parse: Newline must follow cmd in 'eleq "0" struct MessageLogger *Mes
sageLogger::MessageLogger(struct basic_ostream *out); (TranslatorGeneric.cpp
)
'
jpgdbg jpgdbgFileSelector processMsg: Warning unexpected msg token 5
jpgdbg parse: Newline must follow cmd in 'eleq "1" struct MessageLogger *Mes
sageLogger::MessageLogger(char *filename, unsigned char append_to_file); (Tr
anslatorGeneric.cpp)
'
jpgdbg jpgdbgFileSelector processMsg: Warning unexpected msg token 5
I am unable to reproduce this in a sample program where I did the exact same thing as above but everything works fine. Can someone please explain what is happening and if there is a fix to this?
Thanks, Aditya.
Why are you using a dynamically allocated instance of ofstream? Why don't you do something like the following...
This way even if your file fails, you'll still have the log output going to cout...
Back to your problem, what does HW_NEW do? It's kind of hard to say really with the basic information you've provided...