Results 1 to 6 of 6

Thread: EXC_BAD_ACCESS error in redirecting ostream to QTextEdit to make a log window

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2008
    Posts
    10
    Thanks
    1

    Default EXC_BAD_ACCESS error in redirecting ostream to QTextEdit to make a log window

    The class is provided like this,
    Qt Code:
    1. class QDebugStream : public std::basic_streambuf<char>
    2. {
    3. public:
    4. QDebugStream(std::ostream &stream, QTextEdit* text_edit) : m_stream(stream)
    5. {
    6. log_window = text_edit;
    7. m_old_buf = stream.rdbuf();
    8. stream.rdbuf(this);
    9. }
    10. ~QDebugStream()
    11. {
    12. // output anything that is left
    13. if (!m_string.empty())
    14. log_window->append(m_string.c_str());
    15.  
    16. m_stream.rdbuf(m_old_buf);
    17. }
    18.  
    19. protected:
    20. virtual int_type overflow(int_type v)
    21. {
    22. if (v == '\n')
    23. {
    24. log_window->append(m_string.c_str());
    25. m_string.erase(m_string.begin(), m_string.end());
    26. }
    27. else
    28. m_string += v;
    29.  
    30. return v;
    31. }
    32.  
    33. virtual std::streamsize xsputn(const char *p, std::streamsize n)
    34. {
    35. m_string.append(p, p + n);
    36.  
    37. std::string::size_type pos = 0;
    38. while (pos != std::string::npos)
    39. {
    40. pos = m_string.find('\n');
    41. if (pos != std::string::npos)
    42. {
    43. std::string tmp(m_string.begin(), m_string.begin() + pos);
    44. log_window->append(tmp.c_str());
    45. m_string.erase(m_string.begin(), m_string.begin() + pos + 1);
    46. }
    47. }
    48.  
    49. return n;
    50. }
    51.  
    52. private:
    53. std::ostream &m_stream;
    54. std::streambuf *m_old_buf;
    55. std::string m_string;
    56. QTextEdit* log_window;
    57. };
    To copy to clipboard, switch view to plain text mode 

    The Usage is like this:
    Qt Code:
    1. [...]
    2. QTexEdit* myTextEdit = new QTextEdit(this, "myTextEdit");
    3. myTextEdit->setTextFormat(Qt::LogText);
    4.  
    5. QDebugStream qout(std::cout, myTextEdit);
    6.  
    7. std::cout << "Send this to the Text Edit!" << endl;
    8. [...]
    To copy to clipboard, switch view to plain text mode 

    After the EXC_BAD_ACCESS error, GDB backtrace shows:
    #0 0x013b8819 in QTextEdit::append ()
    #1 0x000108e7 in QDebugStream::overflow (this=0xbffff390, v=10) at qDebugStream.h:76
    frame 1 is at:
    " log_window->append(m_string.c_str()); "

    I guess the problem is from the header file. Anyone could help me? I will appreciate that.
    Last edited by owen_263; 9th July 2008 at 05:46. Reason: add the email notification

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.