Results 1 to 17 of 17

Thread: segmentation fault in QT application

  1. #1
    Join Date
    Mar 2007
    Location
    Bangalore
    Posts
    21
    Qt products
    Qt3
    Platforms
    Unix/X11
    Thanks
    1

    Default segmentation fault in QT application

    Hi ,
    I have an application developed in qt and when i run it i get segmentation fault the piece of code that generates the error is as follows:
    Qt Code:
    1. void OffLineKDFWindow::ReadKAMALOffLineFile()
    2. {
    3. char str[256] , *ptr;
    4. char *cptr;
    5. QString s = QFileDialog::getOpenFileName(
    6. "/home",
    7. "Images (*.gsf)",
    8. this,
    9. "open file dialog",
    10. "Choose a .gsf file to open" );
    11. if(s != NULL)
    12. {
    13. strcpy(str,s);
    14. int row = 0;
    15. int col = 0;
    16. GULAB_FILE_t *gfp = NULL;
    17. QString KDFline;
    18. int retval_size=1;
    19. GULAB_PACKET_t gsf_row;
    20. int i, PCount,err;
    21.  
    22. unsigned char formatted_mac_address[18];
    23.  
    24. table->setColumnWidth (0, 100 );
    25. table->setColumnWidth (1, 100 );
    26. table->setColumnWidth (2, 140 );
    27. table->setColumnWidth (3, 160 );
    28. table->setColumnWidth (4, 150 );
    29. table->setColumnWidth (5, 150 );
    30. table->setColumnWidth (6, 120 );
    31. table->setColumnWidth (7, 200 );
    32. table->setColumnWidth (8, 200 );
    33. table->setColumnWidth (9, 120 );
    34. table->setColumnWidth (10, 150 );
    35. table->setColumnWidth (11,6000 );
    36.  
    37. int noRows , noColumns;
    38. noRows = table->numRows();
    39. noColumns = table->numCols();
    40.  
    41. char arr[] = "GULAB off line File reader:";
    42. strcat(arr, s);
    43. setCaption(tr(arr));
    44. if((gfp = gsf_open((unsigned char*)str, READ, NULL, INIT_YES))== NULL)
    45. {
    46. printf("3 Compiler is here..%d\n",gfp->header.total_session_count);
    47. return;
    48. }
    49. for(i=0;i<gfp->header.total_session_count;i++)
    50. {
    51. char tempData[12000] = { 0 };
    52. if((err=gsf_read_next_packet(gfp,(GULAB_PACKET_t *)tempData)) == 0)
    53. {
    54. {
    55. struct in_addr temp;
    56. memcpy((char *)&gsf_row,tempData,sizeof(GULAB_PACKET_t));
    57. temp.s_addr= gsf_row.gph.session_details.source_ip_address;
    58. printf("Setting Text\n");
    59. table->setText( row, 1, (char *)inet_ntoa(temp));
    60. printf("Set text\n");
    61. }
    62. }
    63. printf("Out of if\n");
    64. }
    65. }
    66. printf("Out of 2nd if\n");}
    67. }
    To copy to clipboard, switch view to plain text mode 

    There is no problem with gsf_read_next_packet() function the printfs "Setting Text" and "Set text" is getting printed it comes out of for loop but the statement "Out of 2nd if" does not get printed it is where i get segmentation fault.I have included all the required header files.The point of error is after "Out of if\n" statement.can any one help me
    Last edited by wysota; 12th April 2007 at 14:40. Reason: missing [code] tags

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

    Default Re: segmentation fault in QT application

    Most likely this happens because:

    Qt Code:
    1. if(s != NULL)
    2. {
    3. strcpy(str,s);
    4. int row = 0;
    5. int col = 0;
    6. ...
    To copy to clipboard, switch view to plain text mode 
    You should modify it to:
    Qt Code:
    1. if(!s.isNull())
    2. {
    3. strcpy(str,s.toAscii().toConstData());
    4. int row = 0;
    5. int col = 0;
    6. ...
    To copy to clipboard, switch view to plain text mode 
    QString does not have an ( const char* operator ).
    It crashed when it tried to delete str ( at scope exit ).

    Regards

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: segmentation fault in QT application

    You can't strcpy a QString onto a char*.

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

    Default Re: segmentation fault in QT application

    You can't strcpy a QString onto a char*.
    Not a QString, but a QString::toAscii().constData will work. It's tested.

    From Assistant ( about QByteArray::constData() ):
    This function is mostly useful to pass a byte array to a function that accepts a const char *.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: segmentation fault in QT application

    I meant the thread starter, not you Sorry for confusion.

    Anyway mixing C and C++ this way is not a good idea...

  6. #6
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: segmentation fault in QT application

    Quote Originally Posted by wysota View Post
    Anyway mixing C and C++ this way is not a good idea...
    And even in pure C using strcopy is a mortal sin.

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

    Default Re: segmentation fault in QT application

    No problem...
    To mahiapkum:

    Another error:
    Qt Code:
    1. strcat( arr, s );
    To copy to clipboard, switch view to plain text mode 
    You should also use s.toAscii.constData().

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: segmentation fault in QT application

    Quote Originally Posted by Kumosan View Post
    And even in pure C using strcopy is a mortal sin.
    That's not true. It depends where the data comes from. If it doesn't come from user input (or some other external source), it's safe to use strcpy. And there is always strncpy...

  9. #9
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: segmentation fault in QT application

    Quote Originally Posted by wysota View Post
    That's not true. It depends where the data comes from.
    Yeah, and later on the design changes, e.g. a data source is added or part of this code is reused somewhere else. Even if this cannot happen, I'd be very careful. Experienced programmers may be able to use strcopy safely under normal circumstances. But can they also use it safely when a bunch of crackers with criminal intend try to break your code on purpose? IMHO strcopy should not be used. Never.

    Quote Originally Posted by wysota View Post
    And there is always strncpy...
    Right. And this is the reason why strcopy not only should not be used, but why it is also not necessary to use it.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: segmentation fault in QT application

    Quote Originally Posted by Kumosan View Post
    Yeah, and later on the design changes, e.g. a data source is added or part of this code is reused somewhere else. Even if this cannot happen, I'd be very careful. Experienced programmers may be able to use strcopy safely under normal circumstances. But can they also use it safely when a bunch of crackers with criminal intend try to break your code on purpose? IMHO strcopy should not be used. Never.
    This is the same argument against using "goto". I say that if you know how and when to use a statement, you may safely use it regardless of what others say


    Right. And this is the reason why strcopy not only should not be used, but why it is also not necessary to use it.
    Sure, but it's there to use it so I don't see a reason not to use it. C++ is also not a must - you can code everything in assembly, but rarely people decide to do this now.

  11. #11
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: segmentation fault in QT application

    Quote Originally Posted by wysota View Post
    This is the same argument against using "goto". I say that if you know how and when to use a statement, you may safely use it regardless of what others say
    I don't like this example. A single "goto" does not have the same potential security risks than a single "strcopy". A badly used "goto" might corrupt your design, making you program harder to read and to maintain. A badly used "strcopy" can compromise your whole system.

    And just because it exists. it does not mean it should be used. Computer science evolved with time and "strcopy" is very very old.

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: segmentation fault in QT application

    Quote Originally Posted by Kumosan View Post
    I don't like this example. A single "goto" does not have the same potential security risks than a single "strcopy".
    Sure it has, it can lead straight to stack corruption if you "goto" into another function.

  13. #13
    Join Date
    Mar 2007
    Location
    Bangalore
    Posts
    21
    Qt products
    Qt3
    Platforms
    Unix/X11
    Thanks
    1

    Default Re: segmentation fault in QT application

    but when i do so i get a compilation error of 'toAscii() is not a member of QString'.

  14. #14
    Join Date
    Mar 2007
    Location
    Bangalore
    Posts
    21
    Qt products
    Qt3
    Platforms
    Unix/X11
    Thanks
    1

    Default Re: segmentation fault in QT application

    my qt version is 3.3.3

  15. #15
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 21 Times in 21 Posts

    Default Re: segmentation fault in QT application

    Yes, toAscii() is a Qt4 method ;-)

    With Qt3, simply use this : str.ascii()

    http://doc.trolltech.com/3.3/qstring.html#ascii

  16. #16
    Join Date
    Mar 2007
    Location
    Bangalore
    Posts
    21
    Qt products
    Qt3
    Platforms
    Unix/X11
    Thanks
    1

    Default Re: segmentation fault in QT application

    Thanx i used ascii() and it worked fine.Thanks once again.

  17. #17
    Join Date
    Mar 2007
    Location
    Bangalore
    Posts
    21
    Qt products
    Qt3
    Platforms
    Unix/X11
    Thanks
    1

    Default Re: segmentation fault in QT application

    Hi Marcel thanks 4 ur guidance now i am not getting segmentation fault.

Similar Threads

  1. Strange segmentation fault
    By Lykurg in forum Qt Programming
    Replies: 3
    Last Post: 4th January 2009, 19:50
  2. Segmentation fault in QListViewItem
    By ederbs in forum Qt Programming
    Replies: 2
    Last Post: 26th November 2006, 01:19
  3. why does qt program meet segmentation fault?
    By wquanw in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 15th May 2006, 16:52
  4. Replies: 2
    Last Post: 25th March 2006, 06:54
  5. Icons missing => segmentation fault
    By antonio.r.tome in forum Qt Programming
    Replies: 4
    Last Post: 8th March 2006, 16:30

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
  •  
Qt is a trademark of The Qt Company.