PDA

View Full Version : QList append() problem



reuabreliz
27th December 2009, 19:40
First here is my code:


//My_Highlightingrule.h


class My_Highlightingrule
{
public:
My_Highlightingrule(QRegExp* ,QTextCharFormat* );
QRegExp* getPattern();
QTextCharFormat* getFormat();


private:
QRegExp* pattern;
QTextCharFormat* format;
};



//My_Highlightingrule_KeyWords.h


class My_Highlightingrule_KeyWords : public QList<My_Highlightingrule*>
{
public:
My_Highlightingrule_KeyWords();
};


//My_Highlightingrule_KeyWords.cpp


#include "my_highlightingrule_keyWords.h"


My_Highlightingrule_KeyWords::My_Highlightingrule_ KeyWords()
{
const int MAX = 51;
std::string keyWords[MAX] = {"abstract", "continue", "for", "new",
"switch", "assert", "default", "goto",
"package", "synchronized", "boolean",
"do", "if", "private", "this", "break",
"double", "implements", "protected",
"throw", "byte", "else", "import",
"public", "throws", "case", "enum",
"instanceof", "return", "transient",
"catch", "extends", "int", "short",
"try", "char", "final", "interface",
"static", "void", "class", "finally",
"long", "strictfp", "volatile", "const",
"float", "native", "super", "while"};

QTextCharFormat format;
QBrush brush(QColor(255,0,0,127));
format.setForeground(brush);

QRegExp *pattern;

for(int i = 0; i < MAX; i++)
{
pattern = new QRegExp(QString("\\b%1\\b").arg(keyWords[i].c_str()));
My_Highlightingrule *rule = new My_Highlightingrule(pattern,&format);
insert(i,rule);
}

std::cout << at(49)->getPattern()->pattern()->toStdString() << std::endl; //NO SEGMENTATION FAULT

delete(pattern);
}




//My highlight function...does not highlight now


void My_CppSyntaxHighlighter::highlight(const QString & text)
{
QList<My_Highlightingrule*> rules;
My_Highlightingrule_KeyWords keyWords = y_Highlightingrule_KeyWords();

std::cout << keyWords.at(49)->getPattern()->pattern()->toStdString() << std::endl; //SEGMENTATION FAULT


rules += keyWords; //KeyWords, and so on
}





Ahhm, as you see I have not inserted the #includes and so on.
What I want:
I want a Class My_CppHIghlightingrules which derive from My_SyntaxHighlighting (Is only a Abstract class with a member highlight(const QString&). The function highlight(const QString& text) in My_CppHighlightingrules should add some Highlightingrules (e.g.: My_Highlighting_KeyWords) and then go with a loop through all rules and return the new text.
Problem:
My Problem is this. When I create an object of My_Highlightingrule_Keywords in highlight(const QString&) and I want the last Keyword "while" (see My_Highlightingrule_Keywords() ) then it gives me an Segmentation fault. But when I print the last Keyword "while" in My_Highlightingrule_KeyWords (see My_Highlighting_Keywords() ) then it gives me no Segmentation fault. WHY????? I don't understand it. Please help me.

Tanuki-no Torigava
27th December 2009, 23:00
Did you try to debug? If you use Linux - try GDB.

high_flyer
28th December 2009, 15:40
Are you sure that '49' is a valid value?

Try the follwoing code:


int iIndex = 49;
if(iIndex < keyWords.size()){
std::cout << keyWords.at(iIndex)->getPattern()->pattern()->toStdString() << std::endl;
}else std::cout<<"The index:"<<iIndex<<" is not allowed to be larger than:"<< keyWords.size()<<std::endl;

reuabreliz
2nd January 2010, 11:57
I tried it in every constellation and it is for sure a right value. I mean it is a right value in the constructor My_Highlightingrule_Keywords, but in my Object it is not. I DON'T understand it. It is not logical for me.
I am open for every possibility :)

numbat
2nd January 2010, 12:13
Your My_Highlightingrule class definition suggests you are storing both pattern and format. However, you delete pattern soon after and format is allocated on the stack. Meaning neither can be stored unless you've done a deep copy. If you need further help, how about posting the constructor for My_Highlightingrule.

Coises
2nd January 2010, 22:58
What happens if you remove the delete statement at the end of the My_Highlightingrule_KeyWords constructor?

Are you sure you correctly copied MAX=51 (that it is not MAX=50)?

reuabreliz
6th January 2010, 14:27
Your My_Highlightingrule class definition suggests you are storing both pattern and format. However, you delete pattern soon after and format is allocated on the stack. Meaning neither can be stored unless you've done a deep copy. If you need further help, how about posting the constructor for My_Highlightingrule.

Oh my god. I must be more careful by handling with pointers. Now it works fine.

Thanks a lot for the replies.

My new constructer:




My_Highlightingrule_KeyWords::My_Highlightingrule_ KeyWords()
{
const int MAX = 50;
std::string keyWords[MAX] = {"abstract", "continue", "for", "new",
"switch", "assert", "default", "goto",
"package", "synchronized", "boolean",
"do", "if", "private", "this", "break",
"double", "implements", "protected",
"throw", "byte", "else", "import",
"public", "throws", "case", "enum",
"instanceof", "return", "transient",
"catch", "extends", "int", "short",
"try", "char", "final", "interface",
"static", "void", "class", "finally",
"long", "strictfp", "volatile", "const",
"float", "native", "super", "while" };

QTextCharFormat *format = new QTextCharFormat();
QBrush brush(QColor(255,0,0,127));
format->setForeground(brush);

QRegExp *pattern;

for(int i = 0; i < MAX; i++)
{
pattern = new QRegExp(QString("\\b%1\\b").arg(keyWords[i].c_str()));
My_Highlightingrule *rule = new My_Highlightingrule(pattern,format);
insert(i,rule);
}
}