Results 1 to 6 of 6

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

  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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

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

    What happens if you replace "QTextEdit* log_window" with "QPointer< QTextEdit > log_window" in line 56?

  3. #3
    Join Date
    Mar 2008
    Posts
    10
    Thanks
    1

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

    I tried, but the bug remains~ The "qDebugStream.h" is from 2005's qt forum thread, old enough to introduce bugs for the Newer Qt framework... If we can update this header.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

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

    Quote Originally Posted by owen_263 View Post
    I tried, but the bug remains~
    Did the backtrace change?

  5. #5
    Join Date
    Mar 2008
    Posts
    10
    Thanks
    1

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

    Yes, the backtrace doesn't change at all~
    Sorry for delay for the reply. Sometimes i can't write on the forum, it's really weird.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

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

    What happens if you add "if( ! log_window.isNull() )" before every log_window->append(...) (and of course keep the QPointer<> in the class definition)?

    Do you use threads?

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.