Results 1 to 7 of 7

Thread: more of a C++ question

  1. #1
    Join Date
    Jul 2010
    Posts
    63
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default more of a C++ question

    i have a class derived from QPlainTextEdit.
    i added a member of type QList<[my struct]> to it. since that, the program fails to exit properly ( exit with code -1073741819 after 10-15 seconds of hold ) in debug mode.

    any idea why?

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: more of a C++ question

    Post the code of your class please.

    It's an access violation. Maybe you're trying to access an item in the list that doesn't exist (like in going beyond the item count of the list)

  3. #3
    Join Date
    Jul 2010
    Posts
    63
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: more of a C++ question

    the thing is, it only happens in debug runs when i quit the program; release quits fine- i just cant find any logic to that.
    the class is quite big, that was the reason i didn't post it in the first place. the structs involvement is somewhat minimal.

    its somewhat disorganized since its still work in progress Frankenstein test subject..
    i tried to cut the code down to only where the new list is relevant, which is still not small

    the list i was refering to is "QList<folder> folders;"
    it holds QChars for code folding (kinda)
    Qt Code:
    1. #ifndef CODEEDITOR_H
    2. #define CODEEDITOR_H
    3.  
    4. #include<QPlainTextEdit>
    5. #include<QObject>
    6. #include<QTextBlockUserData>
    7. #include<QtGui>
    8.  
    9. class Highlighter;
    10. class LineNumberArea;
    11.  
    12. struct folder
    13. {
    14. QChar open;
    15. QChar close;
    16. };
    17.  
    18. //--------------------------------------
    19. class CodeEditor : public QPlainTextEdit
    20. {
    21. Q_OBJECT
    22. public:
    23. CodeEditor(QWidget* parent = 0);
    24.  
    25. void lineNumberAreaPaintEvent(QPaintEvent* event);
    26. int lineNumberAreaWidth();
    27.  
    28. QCompleter* completer() const {return mCompleter;}
    29. Highlighter* highlighter() const {return mHighligher;}
    30.  
    31. private:
    32. bool event(QEvent *e);
    33. void resizeEvent(QResizeEvent *e);
    34. void focusInEvent(QFocusEvent *e);
    35. void focusOutEvent(QFocusEvent *e);
    36. void keyPressEvent(QKeyEvent *e);
    37. void bracingEvent();
    38. void homePressedEvent();
    39. void tabPressedEvent(bool shiftPressed);
    40.  
    41. int findTwinBehind( folder req );
    42. int findTwinAhead( folder req );
    43. QString documentCommentClean(int start, int end);
    44. QString indent();
    45. void popCompleter();
    46. void informCompleter();
    47.  
    48. private slots:
    49. void updateLineNumberAreaWidth(int newBlockCount);
    50. void makeExtraSelections();
    51. void updateLineNumberArea(const QRect &, int);
    52. void insertCompletion(const QString& completion);
    53.  
    54. private:
    55. QWidget* lineNumberArea;
    56. Highlighter* mHighligher;
    57. QCompleter* mCompleter;
    58. QList<folder> folders;
    59. };
    60. //--------------------------------------
    61.  
    62. #endif // CODEEDITOR_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "codeEditor.h"
    2. #include"highlighter.h"
    3.  
    4. CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent)
    5. {
    6. QStringList completionList;
    7. completionList << "for" << "while" << "do" << "if" << "else";
    8.  
    9. lineNumberArea = new LineNumberArea(this);
    10. mHighligher = new Highlighter(this->document());
    11.  
    12. mCompleter = new QCompleter(this);
    13. mCompleter->setWidget(this);
    14. mCompleter->setCompletionMode(QCompleter::PopupCompletion);
    15. mCompleter->setModel(new QStringListModel(completionList,mCompleter));
    16.  
    17. connect(this, SIGNAL(blockCountChanged(int)),
    18. this, SLOT(updateLineNumberAreaWidth(int)));
    19.  
    20. connect(this, SIGNAL(updateRequest(const QRect &, int)),
    21. this, SLOT(updateLineNumberArea(const QRect &, int)));
    22.  
    23. connect(this, SIGNAL(cursorPositionChanged()),
    24. this, SLOT(makeExtraSelections()));
    25.  
    26. connect(mCompleter, SIGNAL(activated(QString)),
    27. this, SLOT(insertCompletion(QString)));
    28.  
    29. setStyleSheet("QPlainTextEdit { selection-background-color: darkgray }");
    30. updateLineNumberAreaWidth(0);
    31.  
    32. folder fold;
    33. fold.open = '{';
    34. fold.close = '}';
    35. folders.append(fold);
    36.  
    37. fold.open = '(';
    38. fold.close = ')';
    39. folders.append(fold);
    40.  
    41. fold.open = '[';
    42. fold.close = ']';
    43. folders.append(fold);
    44. }
    45.  
    46. // ::::::: GENERAL METHODS :::::::
    47. //---------------------------------------------------------------------------
    48.  
    49. void CodeEditor::makeExtraSelections()
    50. {
    51. QList<QTextEdit::ExtraSelection> extraSelections;
    52.  
    53. QTextEdit::ExtraSelection* selection;
    54.  
    55. // make current line glow yellow ( not related to the syntax highlighter )
    56. //-----------------------------------------------------------
    57. selection = new QTextEdit::ExtraSelection;
    58. selection->format.setBackground(QColor(Qt::yellow).lighter(160));
    59. selection->format.setProperty(QTextFormat::FullWidthSelection, true);
    60. selection->cursor = textCursor();
    61. selection->cursor.clearSelection();
    62. extraSelections.append(*selection);
    63. //-----------------------------------------------------------
    64.  
    65. // highlight parenthesis under cursor and its twin (behind or ahead)
    66. //-----------------------------------------------------------
    67. foreach( folder cur_fold, folders )
    68. {
    69. if( document()->characterAt(textCursor().position()-1) ==cur_fold.close)
    70. {
    71. selection = new QTextEdit::ExtraSelection() ;
    72. selection->cursor = textCursor();
    73. selection->cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor, 1);
    74. selection->format.setForeground(QColor(Qt::red));
    75. extraSelections.append(*selection);
    76.  
    77. selection = new QTextEdit::ExtraSelection() ;
    78. selection->cursor = textCursor();
    79. selection->cursor.setPosition(findTwinBehind(cur_fold)+1);
    80. selection->cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, 1);
    81. selection->format.setForeground(QColor(Qt::red));
    82. extraSelections.append(*selection);
    83. }
    84. if( document()->characterAt(textCursor().position()-1) ==cur_fold.open)
    85. {
    86. selection = new QTextEdit::ExtraSelection() ;
    87. selection->cursor = textCursor();
    88. selection->cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor, 1);
    89. selection->format.setForeground(QColor(Qt::red));
    90. extraSelections.append(*selection);
    91.  
    92. selection = new QTextEdit::ExtraSelection() ;
    93. selection->format.setForeground(QColor(Qt::red));
    94. selection->cursor = textCursor();
    95. selection->cursor.setPosition(findTwinAhead(cur_fold)-1);
    96. selection->cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, 1);
    97. extraSelections.append(*selection);
    98. }
    99. }
    100. //-----------------------------------------------------------
    101.  
    102. setExtraSelections(extraSelections);
    103. }
    104. QString CodeEditor::documentCommentClean(int start, int end)
    105. {
    106. .........
    107. //replace comment character with '@'
    108. ..........
    109. }
    110.  
    111.  
    112.  
    113. // methods for finding matching pairs of expressions (parenthesis)
    114. int CodeEditor::findTwinBehind( folder req )
    115. {
    116. QString cleaned = documentCommentClean(0, textCursor().position());
    117.  
    118. int i = cleaned.size()-2;
    119. int braceBalance = -1;
    120.  
    121. while( i>0 && braceBalance<0 )
    122. {
    123. if(cleaned.at(i)==req.close) braceBalance-=1;
    124. if(cleaned.at(i)==req.open) braceBalance+=1;
    125. i--;
    126. }
    127. return i;
    128. }
    129.  
    130. int CodeEditor::findTwinAhead( folder req )
    131. {
    132. QString cleaned = documentCommentClean(textCursor().position(), document()->characterCount());
    133.  
    134. int i = 0;
    135. int braceBalance = 1;
    136.  
    137. while( i<cleaned.size() && braceBalance>0 )
    138. {
    139. if(cleaned.at(i)==req.close) braceBalance-=1;
    140. if(cleaned.at(i)==req.open) braceBalance+=1;
    141. i++;
    142. }
    143. return i+textCursor().position();
    144. }
    145.  
    146.  
    147. void CodeEditor::focusInEvent(QFocusEvent *e)
    148. {
    149. setReadOnly(false);
    150. makeExtraSelections();
    151. QPlainTextEdit::focusInEvent(e);
    152. }
    153.  
    154.  
    155. void CodeEditor::focusOutEvent(QFocusEvent *e)
    156. {
    157. setReadOnly(true);
    158. QList<QTextEdit::ExtraSelection> extraSelections;
    159. setExtraSelections(extraSelections);
    160. QPlainTextEdit::focusOutEvent(e);
    161. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jajdoo; 29th July 2010 at 20:04.

  4. #4
    Join Date
    Jul 2010
    Posts
    63
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: more of a C++ question

    lets talk more basic - even when it is not referenced anywhere in the code, its very existence seems to disallow proper quitting exclusively in debug. what could be causing this?
    i know i sound vague, but there isn't really much to it; if its there it crashes at exit in debug, if not it doesn't..

  5. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: more of a C++ question

    Post a small compilable example that causes the problem.

  6. #6
    Join Date
    Jul 2010
    Posts
    63
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: more of a C++ question

    switched from MinGW to MSVC compilation; seems to have resolved the issue..

  7. #7
    Join Date
    Jun 2007
    Posts
    19
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: more of a C++ question

    I have seen something like this before, very hard to find. I think what helped was using .value(var) instead of .at(var), it's null pointer safe.

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.