Results 1 to 6 of 6

Thread: Program crashes while trying to do a check in constructor

  1. #1
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Program crashes while trying to do a check in constructor

    Hi, I have 2 .ui files. One is the main and the other is the preferences window(this is actually a QDialog). In the constructor of the preferences window i have added this code to check if the second line of a file contains the char "1". Unfortunately then the program craches (this is the reason for sure, because when I comment this piece of source then it works fine...
    This is the source:
    Qt Code:
    1. char homee[150]="/home/";
    2. strcat(homee,py); //py is the current logged on user
    3. char otherr[100]="/.config/on_launch_enabled_options";
    4. strcat(homee,otherr);
    5. char line [ 128 ];
    6. int i;
    7. FILE *file = fopen ( homee, "r" );
    8. for ( i = 0; i < 1; ++i )
    9. {
    10. fgets ( line, sizeof line, file ); }
    11. fclose ( file );
    12. if(!strcmp (line,"1\n"))
    13. ui->checkminimizeonlaunch->setCheckState(Qt::Checked); else ui->checkminimizeonlaunch->setCheckState(Qt::Unchecked);
    To copy to clipboard, switch view to plain text mode 
    Thx for any replies.
    Notice that if I do the same thing in a Qt-console application it works fine
    (changing ui->checkminimizeonlaunch->setCheckState(Qt::Checked); else ui->checkminimizeonlaunch->setCheckState(Qt::Unchecked); to cout << "Strings Are similar\n"; )

    It crushes exactly at the point it says fgets ( line, sizeof line, file );
    Last edited by wysota; 1st September 2010 at 09:45.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Program crashes while trying to do a check in constructor

    The file pointer will not be valid.

    Before using pointers, always check if they are valid.

  3. #3
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Program crashes while trying to do a check in constructor

    You are right. After a check the file pointer is not valid.


    So, should I simply change the name of the pointer?

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Program crashes while trying to do a check in constructor

    I don't know what you mean by name of the pointer. The filename, or the object name?

    file = 0 because there's a problem opening the file:

    RETURN VALUES
    Upon successful completion fopen(), fdopen(), and freopen() return a FILE
    pointer. Otherwise, NULL is returned and the global variable errno is
    set to indicate the error.
    Use errno to see which error occured.
    http://www.manpagez.com/man/3/fopen/

    And print out the exect file path that you're trying to open, maybe there's something wrong with it.

  5. #5
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Program crashes while trying to do a check in constructor

    It isn't just pointers. When a routine can fail, you need to check status upon return before proceeding. In this case, you're trying to open a file - an operation that is not guaranteed to succeed. The filename may be wrong; permissions may not allow you to open the file; the disk the file is on my not be mounted; just to name a few of the possible failure modes.

  6. #6
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Program crashes while trying to do a check in constructor

    In a very strange way the program now works fine. i can't understand why but errno returned Success and the Preferences dialog opened normally. Anyway, thx for the help!

Similar Threads

  1. qstringlist array crashes program
    By chrisb123 in forum Newbie
    Replies: 4
    Last Post: 23rd October 2009, 16:03
  2. Program crashes on creating new dialog
    By eekhoorn12 in forum Qt Programming
    Replies: 2
    Last Post: 11th June 2009, 12:52
  3. program crashes (QtTestRunner)
    By fmariusd in forum Qt Programming
    Replies: 1
    Last Post: 15th December 2008, 10:27
  4. Program crashes (SIGSEGV)
    By Voldemort in forum Qt Programming
    Replies: 47
    Last Post: 21st May 2007, 21:09
  5. Reading from TCP Socket crashes program
    By OnionRingOfDoom in forum Qt Programming
    Replies: 26
    Last Post: 27th January 2006, 20:32

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.