Results 1 to 9 of 9

Thread: Segment fault when using Q3ListBox and QFile

  1. #1
    Join Date
    Feb 2007
    Posts
    47
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Segment fault when using Q3ListBox and QFile

    Hi,all.
    I create a window subwin in one dialog and define Q3ListBox listbox in it.Then I redefine the Q3ListBox in mainwindow as listbox = subwin.listbox,the QFile file is defined in the mainwindow driectly.there are some lines in my code when I add them the error is segment fault.
    The lines are as follows:
    char ReadData[100];
    int i=0;
    int point = listbox->numRows();
    file.seek(0);
    while(!file.atEnd()){
    file.readLine(ReadData,100);
    strTempSource = QString(ReadData);
    list->insertItem(strTempSource,point+i);
    i++;
    }
    Can anyone help me?

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Segment fault when using Q3ListBox and QFile

    You may get a buffer overrun in ReadData because readLine also appends aNULL character at the end of what it reads. Try making it larger ( like 101 ).

    Also, make sure list is a valid pointer. Print it's address with qDebug, or something like this.

    Regards

  3. #3
    Join Date
    Feb 2007
    Posts
    47
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Segment fault when using Q3ListBox and QFile

    Quote Originally Posted by marcel View Post
    You may get a buffer overrun in ReadData because readLine also appends aNULL character at the end of what it reads. Try making it larger ( like 101 ).

    Also, make sure list is a valid pointer. Print it's address with qDebug, or something like this.

    Regards
    Well,I have tested it line by line,and on the first line : while( !file.atEnd()),it can't run properly.
    In a small programm for test,it can run properly.The only difference is I define the Q3ListBox in it directly.
    I have no idea what to do next.Please help!Thank you very much!

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Segment fault when using Q3ListBox and QFile

    Could you post the code where you declare and create the list box?

  5. #5
    Join Date
    Feb 2007
    Posts
    47
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Segment fault when using Q3ListBox and QFile

    Quote Originally Posted by marcel View Post
    Could you post the code where you declare and create the list box?
    Draw the subwindow .h file
    class QAddPlusSource : public QDialog{
    public:
    QAddPlusSource();
    public:
    Q3ListBox *m_lstSource;
    QPushButton *m_btnSourceAdd;
    QPushButton *m_btnSourcePlus;

    };
    subwindow's .cpp
    QAddPlusSource::QAddPlusSource(){

    setGeometry(690,560,568,348);
    m_lstSource = new Q3ListBox(this);
    m_lstSource->setGeometry(QRect(140,20,411,311));

    m_btnSourceAdd = new QPushButton(this);
    m_btnSourceAdd->setGeometry(QRect(40,100,80,27));
    m_btnSourceAdd->setText("Add");
    m_btnSourceAdd->setFont(QFont("Times",13,QFont::Bold));

    m_btnSourcePlus = new QPushButton(this);
    m_btnSourcePlus->setGeometry(QRect(40,180,80,27));
    m_btnSourcePlus->setText("Plus");
    m_btnSourcePlus->setFont(QFont("Times",13,QFont::Bold));
    }
    define in the mainwindow's .h file
    .....
    QAddPlusSource AddPlusSource;
    Q3ListBox *m_lstSource;
    .......
    in the mainwindow's .cpp file
    m_lstSource = new Q3ListBox;
    m_lstSource = AddPlusSource.m_lstSource;
    Thank you marcel very much!

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Segment fault when using Q3ListBox and QFile

    OK. I believe that the dialog is deleted and the list from the main window remains invalid.

    Could you post the part where you show the dialog? How is it declared? As a member or somewhere local.

    Regards

  7. #7
    Join Date
    Feb 2007
    Posts
    47
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Segment fault when using Q3ListBox and QFile

    Quote Originally Posted by marcel View Post
    OK. I believe that the dialog is deleted and the list from the main window remains invalid.

    Could you post the part where you show the dialog? How is it declared? As a member or somewhere local.

    Regards
    The dialog's declaration is as I have said above.
    There is a pushbutton on the mainwindow,when the pushbutton is clicked,then the dialog is shown,just as:
    void Mainwindow::ShowAddPlusSource(){
    AddPlusSource.show();
    .....
    tranform the data on it to some parameters which defined in mainwindow
    show some data on parts of the mainwindow
    }

    I don't use the dialog too much in mainwindow.

  8. #8
    Join Date
    Feb 2007
    Posts
    47
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Segment fault when using Q3ListBox and QFile

    I know my mistake now.In
    file.readLine(ReadData,200);
    I define ReadData as char *ReadData,it is wrong! I should define it as char ReadData[200].
    Thank you marcel for your help!

  9. #9
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Segment fault when using Q3ListBox and QFile

    But in the first post was defined as char ReadData[100]...
    It is pk to define it as char *ReadData, but you should also allocate it with ReadData = new char[200].

    Then, when you don't need it anymore, you must delete it with delete[] ReadData.

    Regards

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.