C++应用程序在实例化 ofstream 对象时崩溃。

发布于 2024-10-02 18:15:09 字数 1921 浏览 2 评论 0原文

我在运行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲世九天 2024-10-09 18:15:09

为什么要使用动态分配的 ofstream 实例?为什么你不做如下的事情...

class Logger
{
  public:
    Logger(ostream& str) : _str(str.rdbuf()) // use the buffer from the stream
    {
      _str << "writing to passed in buffer" << endl;
    }
    Logger(const char* fname, bool append = false) : _str(cout.rdbuf())
    {
      _file.open(fname, (append)? ios::out|ios::app : ios::out);
      if (_file.is_open())
        _str.rdbuf(&_file); // redirects to file, else remains on cout

      _str << "expected to be logging to: " << fname << endl;
    }

    // use as needed

  private:
    filebuf _file;
    ostream _str;
};

这样即使你的文件失败,你仍然会有日志输出到cout...

回到你的问题,HW_NEW 做什么?根据您提供的基本信息很难说清楚......

Why are you using a dynamically allocated instance of ofstream? Why don't you do something like the following...

class Logger
{
  public:
    Logger(ostream& str) : _str(str.rdbuf()) // use the buffer from the stream
    {
      _str << "writing to passed in buffer" << endl;
    }
    Logger(const char* fname, bool append = false) : _str(cout.rdbuf())
    {
      _file.open(fname, (append)? ios::out|ios::app : ios::out);
      if (_file.is_open())
        _str.rdbuf(&_file); // redirects to file, else remains on cout

      _str << "expected to be logging to: " << fname << endl;
    }

    // use as needed

  private:
    filebuf _file;
    ostream _str;
};

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文