PDA

View Full Version : App crashes on error ASSERT: "n" in file ..\..\include\QtGui/private/../../../src/gui



karel007
5th February 2011, 18:22
Hi,
i'm trying to do simple formating function with QTextCursor in QPlainTextEdit, but still don't understand why my example app crashes with error:


Starting C:\Qt\Projects\untitled-build-desktop\debug\untitled.exe...
ASSERT: "n" in file ..\..\include\QtGui/private/../../../src/gui/text/qfragmentmap_p.h, line 291
C:\Qt\Projects\untitled-build-desktop\debug\untitled.exe exited with code 3


I have this simple code:


MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
this->edit=new QPlainTextEdit(this);
this->setCentralWidget(this->edit);
connect(edit->document(),SIGNAL(contentsChange(int,int,int)),thi s,SLOT(contentsChange(int,int,int)));
}

void MainWindow::contentsChange(int position,int charsRemoved,int charsAdded) {
if (this->edit->toPlainText().length()>10) {
QTextCursor *curA=new QTextCursor(this->edit->document()->findBlock(0));
QTextCharFormat charFormat;
charFormat.setFontItalic(true);
curA->setPosition(0,QTextCursor::MoveAnchor);
curA->setPosition(4,QTextCursor::KeepAnchor);
curA->beginEditBlock();
if (curA->hasSelection()) {
curA->mergeCharFormat(charFormat);
curA->clearSelection();
}
curA->endEditBlock();
delete curA;
}
}


I'm trying to create simple function which formats specified number of characters from specified position (something like setFormat(int position,int length)) with QTextCursor, but my function is crashing because of mentioned error, so i have prepared simple example to show you way how i'm trying to do it.

ChrisW67
5th February 2011, 22:14
You are trying to merge italic formatting into a QPlainTextEditor. You need to use QTextEditor for that.

In your contentsChange() method you allocate curA on the heap and delete it at the end. This heap allocation gives you no benefit over a simple local variable stack allocation, but exposes you memory leaks in the event of abnormal program exit.