Results 1 to 20 of 28

Thread: (Another) segmentation fault

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2006
    Location
    Slovenia
    Posts
    33
    Thanks
    5
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: (Another) segmentation fault

    Following that example I changed

    Qt Code:
    1. 1.
    2. void GlavnoO :: pushButton3_clicked()
    3. {
    4. bool ok = FALSE;
    5. ok = povezava.baza->isOpen();
    6. }
    To copy to clipboard, switch view to plain text mode 

    to
    Qt Code:
    1. void GlavnoO::pushButton3_clicked()
    2. {
    3. bool ok = FALSE;
    4. QSqlDatabase *baza = QSqlDatabase::database();
    5. ok = baza->isOpen();
    6. }
    To copy to clipboard, switch view to plain text mode 
    It compiles normally but it crashes with segmentation fault again when I press that button.

    And I have to use pointers cause I have this in my Qt Assistant QSqlDatabase Reference:

    Static Public Members
    QSqlDatabase * addDatabase ( const QString & type, const QString & connectionName = defaultConnection )
    QSqlDatabase * addDatabase ( QSqlDriver * driver, const QString & connectionName = defaultConnection )
    QSqlDatabase * database ( const QString & connectionName = defaultConnection, bool open = TRUE )
    void removeDatabase ( const QString & connectionName )
    void removeDatabase ( QSqlDatabase * db )
    bool contains ( const QString & connectionName = defaultConnection )
    QStringList drivers ()
    void registerSqlDriver ( const QString & name, const QSqlDriverCreatorBase * creator )
    bool isDriverAvailable ( const QString & name )

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: (Another) segmentation fault

    Notice also the sentence in docs:
    If connectionName does not exist in the list of databases, 0 is returned.
    So add a check for null pointer:
    Qt Code:
    1. void GlavnoO::pushButton3_clicked()
    2. {
    3. bool ok = FALSE;
    4. QSqlDatabase *baza = QSqlDatabase::database();
    5. if (baza)
    6. {
    7. ok = baza->isOpen();
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    Lebowski (6th April 2006)

  4. #3
    Join Date
    Apr 2006
    Location
    Slovenia
    Posts
    33
    Thanks
    5
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: (Another) segmentation fault

    Yep, that solves the segmentation fault. But I would still like to know how to acces databse connection which I opened in another form.

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

    Default Re: (Another) segmentation fault

    Quote Originally Posted by Lebowski
    And I have to use pointers
    Sure, you're using Qt3 and I was looking at Qt4 docs.

    But I would still like to know how to acces databse connection which I opened in another form.
    First check if addDatabase returns a valid database handle.

  6. #5
    Join Date
    Apr 2006
    Location
    Slovenia
    Posts
    33
    Thanks
    5
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: (Another) segmentation fault

    I did check with this code in form povezava (povezava.ui.h):
    Qt Code:
    1. bool ok = baza->open();
    2. if (!ok) {QMessageBox::warning (this, "Pozor!!!!!!!!!!!", "not working",1,0,0);}
    3. else {
    4. QMessageBox::information ( this, "Uspeh!!!!!!!!!", "working",1,0,0 );
    5. close();
    6. };
    To copy to clipboard, switch view to plain text mode 
    And it certainly works.

    But now I need to access it from a different form, from file glavnoo.ui.h.

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

    Default Re: (Another) segmentation fault

    So why don't you use QSqlDatabase::database()?

  8. #7
    Join Date
    Apr 2006
    Location
    Slovenia
    Posts
    33
    Thanks
    5
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: (Another) segmentation fault

    I am using that.
    Qt Code:
    1. void GlavnoO::test_clicked()
    2. {
    3. bool ok = FALSE;
    4. QSqlDatabase *baza = QSqlDatabase::database();
    5. if (baza) {
    6. ok = baza->isOpen();
    7. };
    To copy to clipboard, switch view to plain text mode 
    Originally it was without 'if (baza) ...' statement.

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

    Default Re: (Another) segmentation fault

    So what's wrong with that? The form you use it in doesn't matter. You can use QSqlDatabase::database() from anywhere. Just makesure addDatabase() has been invoked earlier.

  10. The following user says thank you to wysota for this useful post:

    Lebowski (6th April 2006)

  11. #9
    Join Date
    Apr 2006
    Location
    Slovenia
    Posts
    33
    Thanks
    5
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: (Another) segmentation fault

    Yep, it does work now!! Thanx all.

    Seems I was missing QSqlDatabase::database() statement so my 'baza' pointer had a random value.

Similar Threads

  1. segmentation fault for no apparent reason
    By rishiraj in forum Newbie
    Replies: 1
    Last Post: 12th February 2009, 11:13
  2. segmentation fault
    By uchennaanyanwu in forum Newbie
    Replies: 3
    Last Post: 31st July 2008, 16:52
  3. Process aborted. Segmentation fault
    By Pragya in forum Qt Programming
    Replies: 3
    Last Post: 30th May 2007, 08:12
  4. Segmentation fault running any QT4 executables
    By jellis in forum Installation and Deployment
    Replies: 7
    Last Post: 19th May 2007, 16:35
  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.