Results 1 to 10 of 10

Thread: vector causing system error/crash

  1. #1
    Join Date
    Apr 2010
    Location
    Singapore
    Posts
    156
    Thanks
    47
    Qt products
    Qt4
    Platforms
    Windows

    Default vector causing system error/crash

    I am using vector in my code but when application is running it causes system error/crash.
    Code in .h file :
    Qt Code:
    1. #include <vector>
    2. using namespace std;
    3.  
    4. bool callVector(vector<float>&);
    5. void func();
    6.  
    7. vector <float> vectorname;
    To copy to clipboard, switch view to plain text mode 

    Code in .cpp file:
    Qt Code:
    1. void myClass::func()
    2. {
    3. if(!callVector(vectorname))
    4. {
    5. qDebug() << " vector error" ;
    6. }
    7. else
    8. {
    9. qDebug()<<"vector success" ;
    10. qDebug()<<"vector [0]" : << vectorname[0]; //**system error/crash HERE **
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 
    System error/crash at line 10 in .cpp.
    Application output:
    vector success
    .exe exited with code -1073741819
    Last edited by babygal; 11th October 2010 at 10:19.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: vector causing system error/crash

    Do you ever add anything to the vector?

  3. #3
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: vector causing system error/crash

    This line:
    Qt Code:
    1. vector <float> vectorname;
    To copy to clipboard, switch view to plain text mode 
    Creates an empty vector of floats (and you try to access the first of them)
    If you create empty vector you can use the push_back(...) member function to add elements to that vector

    Or use it like this:
    Qt Code:
    1. vector <float> vectorname(10); //this construct a vector of floats with 10 elements
    To copy to clipboard, switch view to plain text mode 
    Some documentation for std::vector
    Note that the operator[] doesn't check if the number you write there is in vectorname range, you have to make sure that you "stay" in range (else you get an error), you have the at() member function that checks that.

    Or you can use QVector instead, documentation for QVector or QList

  4. #4
    Join Date
    Apr 2010
    Location
    Singapore
    Posts
    156
    Thanks
    47
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: vector causing system error/crash

    Quote Originally Posted by ChrisW67 View Post
    Do you ever add anything to the vector?
    Yes. Contents are added into the vector container. The size is supposed to be around 2000 . But what I get , the size amounts to around 50 000 000 .

  5. #5
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: vector causing system error/crash

    Then show us the code where you add elements to the vector, and also the error you get might be useful (if you run the debug build you might get a "nicer" error message)

  6. #6
    Join Date
    Apr 2010
    Location
    Singapore
    Posts
    156
    Thanks
    47
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: vector causing system error/crash

    using push_back(.....)

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: vector causing system error/crash

    In the absence of your actual code this is what people assume you are doing:
    Qt Code:
    1. #include <iostream>
    2. #include <vector>
    3. using namespace std;
    4.  
    5. bool callVector(vector<float>& v)
    6. {
    7. v.push_back(1.2);
    8. v.push_back(2.3);
    9. return true;
    10. }
    11.  
    12. int main(int argc, char *argv[])
    13. {
    14. vector<float> vectorname;
    15. cout << vectorname.size() << endl;
    16.  
    17. // The following line is probably what you are doing
    18. // There is no element 0 and ranges are not checked
    19. // cout << "vector [0]\t" << vectorname[0]; //**system error/crash HERE **
    20.  
    21. bool res = callVector(vectorname);
    22. cout << res << "\t" << vectorname.size() << endl;
    23.  
    24. // This works now the vector has something in it
    25. cout << "vector [0]\t" << vectorname[0];
    26. }
    To copy to clipboard, switch view to plain text mode 

    Are you returning true from your callVector() routine when you should not perhaps?
    Last edited by ChrisW67; 13th October 2010 at 23:13.

  8. #8
    Join Date
    Apr 2010
    Location
    Singapore
    Posts
    156
    Thanks
    47
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: vector causing system error/crash

    The code :
    .h
    Qt Code:
    1. #include <vector>
    2. using namespace std;
    3.  
    4. bool callVector(vector<float>&);
    5. void func();
    6.  
    7. vector <float> vectorname;
    8. QString filename = "vector.txt";
    9. QFile file(filename);
    10. QString val;
    11. float v;
    To copy to clipboard, switch view to plain text mode 
    .cpp
    Qt Code:
    1. void myClass::func(){
    2. if(!callVector(vectorname))
    3. {
    4. qDebug() << " vector error" ;
    5. }
    6. else
    7. {
    8. qDebug()<<"vector success" ;
    9. }
    10. if (file.open(QIODevice::WriteOnly | QIODevice::Truncate))
    11. {
    12. for ( int i =0; i<vectorname.size() ; i++)
    13. {
    14. v = vectorname.at(i); //system crash here
    15. val = QString::number(v);
    16. qDebug()<< "val" << val;
    17. char *valtoAsciidata = val.toAscii().data();
    18. file.write(valtoAsciidata);
    19.  
    20.  
    21. }
    22. qDebug()<<"front =" << vectorname.front();
    23. qDebug()<<"back =" << vectorname.back();
    24. qDebug()<< "vector write finish!:" << "success";
    25. file.close();
    26.  
    27.  
    28. }
    29. else
    30. {
    31. qDebug()<<"error write vector.txt file";
    32. }
    33. }
    To copy to clipboard, switch view to plain text mode 

    The system crash with error as below when debugging:
    <Signal received>
    The inferior stopped because it received a signal from the Operating System
    Signal name : SIGSEGV
    Signal meaning : Segmentation fault
    The callVector is an API and it is called if dll is loaded. The code is in another program and the code is currently not transparent to me.

    The qDebug() returns "vector success" message when callVector is called in my program.
    Last edited by babygal; 14th October 2010 at 11:19.

  9. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: vector causing system error/crash

    Are you absolutely sure that the callVector function is expecting a std::vector and not just space for a a plain old array of floats (i.e a float*)? Is vectorname.size() sensible when callVector returns? When the seg fault occurs what is the value of i?

    If you have the API prototype correct then you need to speak to the author or provider of the DLL.

  10. #10
    Join Date
    Apr 2010
    Location
    Singapore
    Posts
    156
    Thanks
    47
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: vector causing system error/crash

    Q: "When the seg fault occurs what is the value of i?"
    A : When the segmentation fault occurs, the value of i is always 0.

Similar Threads

  1. Qt 4.7 SSE2 causing my app to crash
    By jonks in forum Qt Programming
    Replies: 8
    Last Post: 26th September 2010, 18:53
  2. QThread::wait() causing crash
    By Olliebrown in forum Qt Programming
    Replies: 3
    Last Post: 24th September 2010, 15:24
  3. Weird Windows Vista Permissions Causing Qmake Crash
    By CrazyIvanovich in forum Qt Programming
    Replies: 1
    Last Post: 4th June 2009, 21:52
  4. QTreeWidget->clear() causing crash after sort
    By Chuk in forum Qt Programming
    Replies: 7
    Last Post: 3rd September 2007, 09:59
  5. runtime error <vector>
    By mickey in forum General Programming
    Replies: 10
    Last Post: 5th September 2006, 12:18

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.