Results 1 to 3 of 3

Thread: [SOLVED] QVector iterator seg fault using .begin/.end

  1. #1
    Join Date
    Jan 2011
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Exclamation [SOLVED] QVector iterator seg fault using .begin/.end

    Built an app (don't sue me apple!) very similar to the addressbook tutorial (used QVector not QMap) and having issues using a next and previous function. As far as I can tell when it gets to checking for it == .begin() and changin it = .end() the it crashes instead of going to the end of the vector.

    mpledit.cpp
    Qt Code:
    1. void MplEdit::previousCargo()
    2. {
    3. if (it == MPLVec.begin())
    4. it = MPLVec.end();
    5.  
    6. if (it == MPLVec.end()) {
    7. ui->fileEdit->clear();
    8. ui->categoryEdit->clear();
    9. ui->priceEdit->clear();
    10. ui->massEdit->clear();
    11. ui->volumeEdit->clear();
    12. ui->descEdit->clear();
    13. }
    14.  
    15. ui->fileEdit->setText(it->getFile());
    16. ui->categoryEdit->setText(it->getCategory());
    17. ui->priceEdit->setText(it->getPrice());
    18. ui->massEdit->setText(it->getMass());
    19. ui->volumeEdit->setText(it->getVolume());
    20. ui->descEdit->setText(it->getDesc());
    21.  
    22. it--;
    23. }
    To copy to clipboard, switch view to plain text mode 
    mpledit.h
    Qt Code:
    1. QVector<MPL> MPLVec;
    2. QVector<MPL>::iterator it;
    3. QVector<MPL>::const_iterator sav;
    To copy to clipboard, switch view to plain text mode 

    When i mouseover MPLVec.begin() it tells me that begin is const_iterator QVector::begin const when I *think* it should be telling me iterator QVector::begin () instead. Don't know if thats the issue or not... the const iterator save is only used in the saveCargo function and has no issues. I have tried clean project, removing all references to const_iterator (and just used two QVector:: iterator calls) but so far no luck. If you need me to post additional code let me know.
    Last edited by pheonixstorm; 9th January 2011 at 07:45. Reason: add solved to title to minimize new replies.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QVector iterator seg fault using .begin/.end

    the end() function is returning and iterater behind (= invalid) the vector. See inside the docs for "Container Classes" for a detailed explanation. So your app crashes since it is invalid. Dekrement the iterator and all should work fine.

  3. #3
    Join Date
    Jan 2011
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QVector iterator seg fault using .begin/.end

    Odd considering the same nearly the same code is in the addressbook tutorial
    Qt Code:
    1. if (i == contacts.end()) {
    2. nameLine->clear();
    3. addressText->clear();
    4. return;
    5. }
    6.  
    7. if (i == contacts.begin())
    8. i = contacts.end();
    9.  
    10. i--;
    To copy to clipboard, switch view to plain text mode 

    Ok, nevermind.. I found the issue. Its not a bad iterator but bad iterator placement in the code. After looking at the above code segment I noticed MY placement of it-- was after calling for the information to post in the line edits. So should have read
    Qt Code:
    1. it--;
    2. ui->something->settext(it->variable)
    To copy to clipboard, switch view to plain text mode 
    instead of
    Qt Code:
    1. ui->something->settext(it->variable)
    2. it--;
    To copy to clipboard, switch view to plain text mode 
    Its always the stupid things you miss... Thanks for the info even though I didn't get a chance to read up on it before finding my blunder

Similar Threads

  1. QPainter::begin: Cannot paint on a null pixmap
    By sabeesh in forum Qt Programming
    Replies: 5
    Last Post: 27th July 2010, 18:03
  2. go to file's begin
    By jaca in forum Qt Programming
    Replies: 1
    Last Post: 19th September 2009, 05:59
  3. problem in QPainter::begin
    By wagmare in forum Qt Programming
    Replies: 5
    Last Post: 17th July 2009, 10:31
  4. QHttp RequestStarted does not even begin!
    By Arsenic in forum Qt Programming
    Replies: 6
    Last Post: 9th August 2008, 03:52
  5. Application optimization - Where to begin?
    By Tux-Slack in forum General Programming
    Replies: 2
    Last Post: 19th October 2007, 01:38

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.