Page 1 of 2 12 LastLast
Results 1 to 20 of 28

Thread: (Another) segmentation fault

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

    Default (Another) segmentation fault

    Hello.

    I've started using QT recently and I quite like it so far. However I can't get past this error.

    In Povezava.ui.h I have setup connection to database like this:
    Qt Code:
    1. void Povezava::Connect_clicked()
    2. {
    3. int indeks = tipbaze->currentItem();
    4. if (indeks==0) {driver ="QMYSQL3";}
    5. if (indeks==1) {driver ="QPSQL7";}
    6. if (indeks==2) {driver ="QODBC3";}
    7.  
    8. QSqlDatabase *baza = QSqlDatabase::addDatabase ( driver );
    9. baza->setHostName ( hostm );
    10. baza->setDatabaseName ( namem );
    11. baza->setUserName ( userm );
    12. baza->setPassword ( passm );
    13.  
    14. bool ok = baza->open();
    15. }
    To copy to clipboard, switch view to plain text mode 
    This part definitelly works as I've tested it in many ways.

    In another form I'm checking if the connection is properly opened with this code:
    Qt Code:
    1. void GlavnoO :: pushButton3_clicked()
    2. {
    3. bool ok = FALSE;
    4. ok = povezava.baza->isOpen();
    5. }
    To copy to clipboard, switch view to plain text mode 
    I don't get any errors when compiling. But whet I click on this button I get segmentation fault EVERY time! I've tried it with connection opened and closed. I also did 'make clean' just in case. Nothing worked.

    I have no idea how to get past this.

  2. #2
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: (Another) segmentation fault

    Its ok becouse you declare baza variable in method Connect_clicked(). After returning from this method you havent access to this variable. For solving you need declare baza as class member
    a life without programming is like an empty bottle

  3. The following user says thank you to zlatko 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

    I already have baza declared as class member in povezava.ui.h

    It's under povezava.ui.h->members->class variables->public-> QSqlDatabase *baza ;

  5. #4
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: (Another) segmentation fault

    So why you redeclare it in
    Qt Code:
    1. QSqlDatabase *baza = QSqlDatabase::addDatabase ( driver );
    To copy to clipboard, switch view to plain text mode 

    Right code

    Qt Code:
    1. baza = QSqlDatabase::addDatabase ( driver )
    To copy to clipboard, switch view to plain text mode 
    a life without programming is like an empty bottle

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

    Default Re: (Another) segmentation fault

    Tried that but still getting segmentation fault in the same way

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: (Another) segmentation fault

    Compile your application in debug mode and run it from a debugger. When it crashes, print the stack trace and post it here.

  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've added "CONFIG += debug" into my project file but the output when my application crashes still says only "Segmentation fault".

    This is debug from 'make':
    [root@simon dbconn]# make --debug
    GNU Make 3.80
    Copyright (C) 2002 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.
    There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
    PARTICULAR PURPOSE.
    Reading makefiles...
    Updating goal targets....
    File `first' does not exist.
    File `all' does not exist.
    Prerequisite `povezava.ui.h' is newer than target `.ui/povezava.cpp'.
    Must remake target `.ui/povezava.cpp'.
    /usr/lib/qt-3.3/bin/uic povezava.ui -i povezava.h -o .ui/povezava.cpp
    Successfully remade target file `.ui/povezava.cpp'.
    Prerequisite `.ui/povezava.cpp' is newer than target `.obj/povezava.o'.
    Prerequisite `povezava.ui.h' is newer than target `.obj/povezava.o'.
    Must remake target `.obj/povezava.o'.
    g++ -c -pipe -Wall -W -g -DQT_SHARED -DQT_NO_DEBUG -DQT_THREAD_SUPPORT -I/usr/lib/qt-3.3/mkspecs/default -I. -I/usr/lib/qt-3.3/include -I.ui/ -I. -I.moc/ -o .obj/povezava.o .ui/povezava.cpp
    /usr/lib/qt-3.3/include/qsqldatabase.h:63: warning: ‘class QSqlDriverCreatorBase’ has virtual functions but non-virtual destructor
    /usr/lib/qt-3.3/include/qtooltip.h:86: warning: ‘class QToolTip’ has virtual functions but non-virtual destructor
    Successfully remade target file `.obj/povezava.o'.
    Prerequisite `.obj/povezava.o' is newer than target `povezava'.
    Must remake target `povezava'.
    g++ -o povezava .obj/main.o .obj/glavnoo.o .obj/povezava.o .obj/moc_glavnoo.o .obj/moc_povezava.o -L/usr/lib/qt-3.3/lib -L/usr/X11R6/lib -lqt-mt -lXext -lX11 -lm
    Successfully remade target file `povezava'.
    Must remake target `all'.
    Successfully remade target file `all'.
    Must remake target `first'.
    Successfully remade target file `first'.
    [root@simon dbconn]# ./povezava
    Segmentation fault

  9. #8
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Thanks
    44
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: (Another) segmentation fault

    Just a way to "debug", check everytime before accesing to baza pointer if its null and show a message on screen or on the standard output, for example, in this case.
    Last edited by SkripT; 4th April 2006 at 11:27.

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

    Default Re: (Another) segmentation fault

    Try this:

    $ gdb ./povezava
    (gdb) run
    (some output will go there and eventually the application will segfault)
    (gdb) bt


    and show us the result of "bt".

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

    Default Re: (Another) segmentation fault

    Here is what I get with gdb:
    (gdb) run
    Starting program: /home/simon/projects/dbconn/povezava
    Reading symbols from shared object read from target memory...done.
    Loaded system supplied DSO at 0xb9e000
    [Thread debugging using libthread_db enabled]
    [New Thread -1208837664 (LWP 9818)]

    Program received signal SIGSEGV, Segmentation fault.
    [Switching to Thread -1208837664 (LWP 9818)]
    0x04d45bb5 in QSqlDatabase::isOpen (this=0xffff) at sql/qsqldatabase.cpp:772
    772 return d->driver->isOpen();
    (gdb) bt
    #0 0x04d45bb5 in QSqlDatabase::isOpen (this=0xffff) at sql/qsqldatabase.cpp:772
    #1 0x0804bcb0 in GlavnoO::test_clicked (this=0xbff491fc) at .ui/../glavnoo.ui.h:28
    #2 0x0804e877 in GlavnoO::qt_invoke (this=0xbff491fc, _id=52, _o=0xbff48af8) at .moc/moc_glavnoo.cpp:87
    #3 0x04ab9eb4 in QObject::activate_signal (this=0x9967440, clist=0x9969488, o=0xbff48af8) at kernel/qobject.cpp:2355
    #4 0x04aba374 in QObject::activate_signal (this=0x9967440, signal=4) at kernel/qobject.cpp:2324
    #5 0x04e39c7e in QButton::clicked (this=0xffff) at .moc/release-shared-mt/moc_qbutton.cpp:152
    #6 0x04b59a63 in QButton::mouseReleaseEvent (this=0x9967440, e=0xbff48e54) at widgets/qbutton.cpp:836
    #7 0x04af8187 in QWidget::event (this=0x9967440, e=0xbff48e54) at kernel/qwidget.cpp:4699
    #8 0x04a550dd in QApplication::internalNotify (this=0xffff, receiver=0x9967440, e=0xbff48e54) at kernel/qapplication.cpp:2635
    #9 0x04a5603f in QApplication::notify (this=0xbff49334, receiver=0x9967440, e=0xbff48e54) at kernel/qapplication.cpp:2421
    #10 0x049ec746 in QETWidget::translateMouseEvent (this=0x9967440, event=0xbff49118) at kernel/qapplication.h:518
    #11 0x049eaf01 in QApplication::x11ProcessEvent (this=0xbff49334, event=0xbff49118) at kernel/qapplication_x11.cpp:3468
    #12 0x049ff008 in QEventLoop:rocessEvents (this=0x9944fe0, flags=Variable "flags" is not available.
    ) at kernel/qeventloop_x11.cpp:192
    #13 0x04a6d82b in QEventLoop::enterLoop (this=0x9944fe0) at kernel/qeventloop.cpp:198
    #14 0x04a6d736 in QEventLoop::exec (this=0x9944fe0) at kernel/qeventloop.cpp:145
    #15 0x04a54aa9 in QApplication::exec (this=0xbff49334) at kernel/qapplication.cpp:2758
    #16 0x0804bc31 in main (argc=1, argv=0xbff49414) at main.cpp:19
    (gdb) Quit
    (gdb)

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

    Default Re: (Another) segmentation fault

    Can we see the current code too?

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

    Default Re: (Another) segmentation fault

    Code is still the same as it was in the first post.

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

    Default Re: (Another) segmentation fault

    Then this code is surely invalid as you have a local variable "baza" which points to the database and a member variable "baza" which points to some garbage which probably causes a crash.

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

    Default Re: (Another) segmentation fault

    Possible. But why doesn't compiler find any errors? And how do i change the code to make it work?

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

    Default Re: (Another) segmentation fault

    Why should it find errors? The code is valid C++, it just doesn't do what you wnt. Zlatko already told you how to correct it.

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

    Default Re: (Another) segmentation fault

    You mean this post?
    Quote Originally Posted by zlatko
    So why you redeclare it in
    Qt Code:
    1. QSqlDatabase *baza = QSqlDatabase::addDatabase ( driver );
    To copy to clipboard, switch view to plain text mode 

    Right code

    Qt Code:
    1. baza = QSqlDatabase::addDatabase ( driver )
    To copy to clipboard, switch view to plain text mode 
    I tried that but I'm still getting segmentation fault.

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

    Default Re: (Another) segmentation fault

    But the code is different. Run it under debugger again. And please paste the contents of test_clicked() slot.

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

    Default Re: (Another) segmentation fault

    Hmm, I think I figured out my problem with help from all of you. I need to properly specify 'QSqlDatabase *baza; ' as a global variable. Now the n00b question: how do I do that, please? Tried to do it in my main.cpp but it doesn't work.

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

    Default Re: (Another) segmentation fault

    No, you don't have to do that. First of all QSqlDatabase::addDatabase() returns an object and not a pointer. And second of all you can access the database later on by calling QSqlDatabase::database(). You don't need a variable to hold that handler. Please read the reference of QSqlDatabase. It's explained there (on the very beginning of the detailed description).

  21. #20
    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 )

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.