C++应用程序在实例化 ofstream 对象时崩溃。
我在运行 C++ 应用程序时遇到一个非常恼人的问题。我在 Windows Xp 的 Interix 子系统上使用 pgcpp 编译器。我的问题本质上是在这里描述的:
我在头文件中有一个类定义。该头文件包含在一个源文件中。该类有两个构造函数,主要用于实现记录器。第一个构造函数采用 ostream *out 作为参数,而第二个重载构造函数采用文件名和默认布尔值 false。第二个构造函数的目标是获取我们传递的文件名的流并开始向其中记录消息。构造函数中的代码如下:
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;
}
}
其中p_out 和p_ofstream 的声明如下:
std::ostream *p_out;
std::ofstream *p_ofstream;
unsigned int p_indent_level;
上述三个都是私有成员。 MessageLogger 类的实例化完成如下:
MessageLogger logger ("filename");
请注意,append_to_file 的默认值为 false。 PGDBG
也有不当行为。当控件位于 p_ofstream = new std::ofstream (filename); 时,我莫名其妙地能够介入,并且它进入随机位置,然后应用程序崩溃。
另外,当我尝试在 PGBG 中查看混合或反汇编代码时,调试器崩溃并显示以下消息:
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么要使用动态分配的 ofstream 实例?为什么你不做如下的事情...
这样即使你的文件失败,你仍然会有日志输出到cout...
回到你的问题,HW_NEW 做什么?根据您提供的基本信息很难说清楚......
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...