【问题标题】:Segfault on deleting class member pointer to a stream删除指向流的类成员指针的段错误
【发布时间】:2013-12-30 04:26:17
【问题描述】:

我正在尝试编写一个用于处理异常的类。我认为添加一个

class prc_exception : public std::exception {
  private:
    int line_num;
    std::ostringstream* msg;

  public:
    prc_exception(const char* part, const int line) throw()
    : exception() {
      msg = new std::ostringstream();
      *msg << "<parcer:" << part << ":" << line << ">: ";
    }
    ~prc_exception() throw() { delete msg; }
    virtual const char* what() const throw() { return msg->str().c_str(); }
    virtual const int line() const throw() { return line_num; }
    template <class T> prc_exception& operator<<(const T& rhs)
    { *msg << rhs; return (*this); }
};

另外,如果有人可以提出更好的方法来处理我想做的事情,请给我一个建议。 我想要的是这样可用的东西:

throw prc_exception("code_part",__LINE__) << "More info";

当被捕获时它的 what() 函数会返回一个字符串,如:

<parcer:code_part:36>: More info

非常感谢。

已解决: 正如@polkadotcadaver 建议的那样,添加一个复制构造函数和一个复制赋值运算符解决了这个问题。固定代码按预期运行: #包括 #包括

using namespace std;

class prc_exception : public std::exception {
  private:
    int line_num;
    std::ostringstream msg;

  public:
    prc_exception(const char* part, const int line) throw()
    : exception() {
      msg << "<parcer:" << part << ":" << line << ">: ";
    }
    /** Copy Constructor */
    prc_exception (const prc_exception& other)
    {
      line_num = other.line();
      msg << other.what();
    }
    /** Copy Assignment Operator */
    prc_exception& operator= (const prc_exception& other)
    {
      line_num = other.line();
      msg << other.what();
      return *this;
    }
    ~prc_exception() throw() { }

    virtual const char* what() const throw() { return msg.str().c_str(); }
    virtual const int line() const throw() { return line_num; }
    template <class T> prc_exception& operator<<(const T& rhs)
    { msg << rhs; return (*this); }
};

int main()
{
  try
  {
    throw prc_exception("hello", 5) << "text." << " more text.";
  } catch (exception& e) {
    cout << e.what() << endl;
  }

  return 0;
}

【问题讨论】:

  • 查看copy-and-swap idiom,了解在这种情况下如何编写复制赋值运算符(注意,我没试过!)

标签: c++ class exception stream destructor


【解决方案1】:

下面的答案是一个假设,因为我没有你在哪里使用你的类的代码。如果我叫错了树,请说!

好的,这是一个可编译、可运行的测试程序,它顺利完成。

#include <iostream>
#include <sstream>

using namespace std;

class prc_exception : public std::exception {
  private:
    int line_num;
    std::ostringstream* msg;

  public:
    prc_exception(const char* part, const int line) throw()
    : exception() {
      msg = new std::ostringstream();
      *msg << "<parcer:" << part << ":" << line << ">: ";
    }
    ~prc_exception() throw() { delete msg; }
    virtual const char* what() const throw() { return msg->str().c_str(); }
    virtual const int line() const throw() { return line_num; }
    template <class T> 
    prc_exception& operator<<(const T& rhs)
    { *msg << rhs; return (*this); }
};

int main()
{
    try
    {
        throw prc_exception("hello", 5);
    }
    catch (prc_exception& e)
    {
        e << "Oh dear";

        cout << e.what() << endl;
    }

    return 0;
}

我认为您可能正在做的是按值捕获异常,而不是按引用:

    try
    {
        throw prc_exception("hello", 5);
    }
    catch (prc_exception& e)
    {
        e << "Oh dear";

        cout << e.what() << endl;
    }

...现在它在按预期打印输出后出现段错误。原因是两件事的结合。

首先,您有一个错误的复制构造函数。由于您自己没有定义复制构造函数,因此编译器为您生成了一个。它的默认行为是复制你的类的每个成员——当你有一个像你的 ostringstream 这样的堆分配成员时,这很糟糕。

这样想(不要认为这意味着异常应该是新的,它们不应该!):

// This creates an ostringstream, let's call it Bob
prc_exception* an_exception = new prc_exception{"hello", 5}; 

// This SHARES the first ostringstream, Bob, as it just copied the pointer, 
// NOT what it was pointing to. 
prc_exception* a_second_exception = new prc_exception{an_exception};

// Delete the original exception. This calls ~prc_exception and deletes Bob
delete an_exception;

// WHOOPS we just deleted Bob again, because we were pointing to it. 
// This causes the crash.
delete a_second_exception

当您按值捕获异常时,我怀疑您是这样,这将复制异常。最佳做法是始终通过reference捕获异常。

但至关重要的是,如果您创建一个具有一个或多个堆分配成员的类,您必须还编写自己的复制构造函数、复制赋值运算符和析构函数。谷歌的三法则(参见下面的参考资料,其他细节适用)。

我根本看不出堆分配流的原因。只要有一个 ostringstream 作为成员 - 我怀疑你最初是这样做的,但是在你按值捕获异常的地方出现了编译错误。这是因为 ostringstream 不可复制!

所以你的解决方法很明确:

  • 使 ostringstream 成为一个成员,而无需对其进行更新。废弃析构函数。
  • 通过引用捕获异常。

参考资料:

Problem with ostringstream and copy constructor

http://www.gotw.ca/gotw/069.htm

https://en.wikipedia.org/wiki/Rule_of_three_(C++_programming)

【讨论】:

  • 非常感谢@polkadotcadaver。我学到了一些新东西。由于复制它的问题,我确实将指向流的指针作为成员,您指出了这一点。不过我是通过引用来捕捉的。
猜你喜欢
  • 2021-12-03
  • 2018-03-24
  • 1970-01-01
  • 2011-04-30
  • 2012-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多