January 13, 2011

st.loeffler st.loeffler
Lab Rat
7 posts

Crash when using QTextFormat::FullWidthSelection with word wrap

 

Hi,

I’m one of the programmers of TeXworks, a free LaTeX editor which uses Qt for its interface. Recently, we had bug reports which I believe I have tracked down to the QTextFormat::FullWidthSelection flag we use to highlight the current line. I’ve opened a bug report for that (http://bugreports.qt.nokia.com/browse/QTBUG-16102), but it was closed immediately without any comments. So I’m trying my luck here – hopefully someone can help me.

Here’s my original bug report:
If in a QTextEdit widget, one uses an extra selection with a QTextFormat::FullWidthSelection property (to highlight the current line; as suggested by the docs), and word wrapping is turned on, the application crashes (most of the time) when entering spaces at the end of a line so that the line becomes longer than the widget width (spaces do not seem to be wrapped, though). Apparently, a crash becomes more likely if there are non-whitespace characters close to the (right) edge of the widget. Note also that the cursor apparently must be positioned at the end of the line (i.e., outside the widget area) for the crash to happen. This only seems to affect Windows platforms.

And a minimal sample:

  1. #include <QtGui>
  2.  
  3. class Text: public QTextEdit
  4. {
  5. public:
  6.  Text(QWidget* pParent = 0);
  7.  void paintEvent(QPaintEvent *event);
  8. };
  9.  
  10. Text::Text(QWidget* pParent) : QTextEdit(pParent)
  11. {
  12.  // uncomment the following line to avoid crash
  13.  // this->setWordWrapMode(QTextOption::NoWrap);
  14.  this->setPlainText("                                                                        aaa    ");
  15. }
  16.  
  17. void Text::paintEvent(QPaintEvent *event)
  18. {
  19.  QTextEdit::ExtraSelection highlight;
  20.  QList<QTextEdit::ExtraSelection> extras;
  21.  QList<QTextCursor*> errorCursors;
  22.  QTextCursor originalCursor = this->textCursor();
  23.  QTextCursor cursor = this->textCursor();
  24.  
  25.  // Mark current line
  26.  highlight.cursor = this->textCursor();
  27.  // Comment the following line to avoid crash
  28.  highlight.format.setProperty(QTextFormat::FullWidthSelection, true);
  29.  highlight.format.setBackground(QColor(0,255,0));
  30.  extras << highlight;
  31.  
  32.  // Add extra selections
  33.  // Comment the following line to avoid crash
  34.  this->setExtraSelections(extras);
  35.  
  36.  QTextEdit::paintEvent(event);
  37. }
  38.  
  39. int main(int argc, char *argv[])
  40. {
  41.  QApplication app(argc, argv);
  42.  Text widget;
  43.  widget.show();
  44.  return app.exec();
  45. }

And finally links to the original bugs reported on our issue tracker:
http://code.google.com/p/texworks/issues/detail?id=463&start=200
http://code.google.com/p/texworks/issues/detail?id=464&start=200

Thanks in advance for any help you can offer.

-Stefan

9 replies

January 14, 2011

Andre Andre
Area 51 Engineer
6031 posts

Unfortunately, you can not vote for closed bugs to get them open again by popular demand or something like that. It seems like you have a real issue on your hands. It is beyond me why the bugreport, with a testcase attached, would be closed without a comment.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

January 14, 2011

Volker Volker
Robot Herder
5428 posts

I cannot reproduce a crash on Windows (VS 2008, Debug mode, Qt 4.7.1).

January 15, 2011

st.loeffler st.loeffler
Lab Rat
7 posts

Hi,

first of all thanks for the replies, and for testing.
I’m using Mingw with gcc 4.5.1 & 4.5.2 with Qt 4.7.1 (debug & release) on WinXP and Win7. The crash occurs reproducibly on my machine.
I experimented a bit more and the problem may be related only to the current cursor position. If I take the example code, the resulting line is slightly less wide than the window. If I resize the window so that the widget becomes narrower than the text (but there is no wrapping at the “aaa” yet), I can also produce the crash by moving the cursor into the area that is off-screen (i.e., without adding any characters). The crash only seems to occur when the cursor is close to the widget boundary (i.e., if I move the cursor to the end of a long line, no crash occurs; if I then start moving to the left, eventually the app crashes, presumably when the cursor is at the widget boundary). Again, this does not occur if I comment the use QTextFormat::FullWidthSelection.

Another useful note: When adding spaces, the crash only seems to occur when you do so “slowly” (wait, say, 1 second between adding consecutive spaces). I presume this has to do with the fact that the (usually blinking) cursor visibility must change for the crash to occur? Also, lines consisting only of spaces do not trigger the crash (hence the “aaa” in the example code).

BTW: Does anyone know why there doesn’t seem to be a line wrap for spaces consisting only of spaces?

Thanks in advance for any further insight/help/comments you can offer.

-Stefan

January 25, 2011

st.loeffler st.loeffler
Lab Rat
7 posts

Hi again,

this also seems to affect the script debugger bundled with Qt itself (see http://tug.org/pipermail/texworks/2011q1/003647.html [tug.org]). So this seems to be a very serious issue.

Does anyone have additional ideas/suggestions/comments? Should I try again to open a bug report? Or is this issue known or even fixed in the current development code, perhaps (I didn’t find anything in the bug tracker, but maybe I missed something)?

-Stefan

[EDIT: fixed link, Volker]

January 25, 2011

Volker Volker
Robot Herder
5428 posts

I would open a bug report. If you add your test case from above and instructions how to reproduce the error, it’s more likely that it will be fixed sooner.

January 26, 2011

Zlatomir Zlatomir
Dinosaur Breeder
326 posts

I have tested your code, and Creator warn about recursive repaint():

  1. QWidget::repaint: Recursive repaint detected
  2. QPaintDevice: Cannot destroy paint device that is being painted

Debug your code and make sure the recursion is not infinite, also look in documentation which function calls repaint (probably one from the ones that if you comment solves the issue)

January 30, 2011

st.loeffler st.loeffler
Lab Rat
7 posts

Thanks for all the helpful comments.

@Zlatomir: Unfortunately, I couldn’t reproduce the warnings you got. At which point did they show up (compiling, running, debugging, after the crash,…)? FWIW, my configuration is Qt 4.7.1, mingw debug build with gdb debugger, on Win XP.

Using Qt Creator and gdb, I think I’ve been able to pinpoint the error. Consequently, I’ve opened a new bug report [bugreports.qt.nokia.com] including a backtrace and a proposed patch.

HTH
-Stefan

January 30, 2011

Zlatomir Zlatomir
Dinosaur Breeder
326 posts

I got that warnings at runtime in the Application Output, whenever the cursor is on right edge of the row and the window is resized over the cursor (as far that i understand this is how we should try to replicate the bug)
Using Qt SDK (tech preview) on Win 7 64bit, so no crash here, only warnings
crash_test

January 31, 2011

st.loeffler st.loeffler
Lab Rat
7 posts

OK, seems this bug has been fixed upstream in December.

Thanks for the help, guys.

-Stefan

 
  ‹‹ Select first row in QTableView after selected item is deleted.      [Solved] Enable / disable drag and drop in some part of a qtextedit ››

You must log in to post a reply. Not a member yet? Register here!