Results 1 to 20 of 27

Thread: Problem with SqLite and Qt

Hybrid View

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

    Default Re: Problem with SqLite and Qt

    Quote Originally Posted by jacek View Post
    But you have just agreed with me that you can't really tell what happened.
    It doesn't mean you can't guess in what part of code you are. For example you might track the state of the application. I can imagine you might trap SIGSEGV to try to recover from a problem when using an unknown and assumed dangerous external code (like a plugin or so).

    If my app tends to crash, I debug it to fix the crash.
    Provided you have time and manage to isolate the problem.

    It's good that you agree with me that clean up and termination (instead of fix and continuation) is the only option in the event of SIGSEGV.
    Now that I think of it... no I just remembered that when you use mprotect, access to a protected region of memory causes a segfault, but the app is perfectly fine, so you can easily recover and continue. There is a chance it's not a sole case.

    Remember that the data you want to save might be on that stack.
    Yes, or you might be running a system that doesn't handle signals or find dozens of other cases where it won't work.

  2. #2
    Join Date
    Jun 2007
    Location
    Louisiana
    Posts
    77
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem with SqLite and Qt

    All of this discussion about error trapping is nice and very interesting but it does not tell me why the application gives me the segmentation fault. Please try to stay on point as I am very new at C++ and Qt and it can become very confusing very quickly for me. I am not a veteran C programmer (hense the newbie forum questions).

    I do have the sqlite plugin. It came as I installed SUSE 10.2 development distro, otherwise I would not have been able to run sqlite from the command line.

    I just noticed that the debugger flashes a notice on the status line that there was a shared library event - if that helps any. I also notice that the database object is defined twice. Once as a mainwindow member and again independently. What is that all about? DB is ONLY a mainwindow public member and the createConnection() is in the Mainwindow code. It should not have created a new object independent of mainwindow.

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

    Default Re: Problem with SqLite and Qt

    Quote Originally Posted by ad5xj View Post
    I do have the sqlite plugin. It came as I installed SUSE 10.2 development distro, otherwise I would not have been able to run sqlite from the command line.
    Wrong. You're mixing sqlite library and qsqlite Qt plugin. Jacek was asking about the latter. Please list the content of your plugins/sqldrivers subdirectory of the Qt root install directory.

    I also notice that the database object is defined twice. Once as a mainwindow member and again independently. What is that all about? DB is ONLY a mainwindow public member and the createConnection() is in the Mainwindow code. It should not have created a new object independent of mainwindow.
    Jacek has already pointed that out in one of his previous posts - you declare a local variable, thus the pointer declared in the main window is still invalid - that's the reason of your segmentation fault.

  4. #4
    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: Problem with SqLite and Qt

    Quote Originally Posted by ad5xj View Post
    DB is ONLY a mainwindow public member and the createConnection() is in the Mainwindow code.
    No, it's not. Look:
    Qt Code:
    1. static bool createConnection()
    2. {
    3. ...
    4. }
    To copy to clipboard, switch view to plain text mode 
    createConnection() is a static function, so it's not a method of Mainwindow class, even if it ends up in the same file.

    The second problem is that you do have two DB variables:
    Qt Code:
    1. QSqlDatabase * DB = QSqlDatabase::addDatabase("QSQLITE");
    To copy to clipboard, switch view to plain text mode 
    In this line the "QSqlDatabase * DB" part means "declare a new local variable named DB of QSqlDatabase * type". It should be:
    Qt Code:
    1. pointerToMainwindow->DB = QSqlDatabase::addDatabase("QSQLITE");
    To copy to clipboard, switch view to plain text mode 
    Although you should rather make createConnection() a method of Mainwindow class, if openDB() is already there.

    But before you change your code further, make sure that Qt is configured correctly on your system. If everything is OK with your system, the sqlbrowser demo should list "QSQLITE" driver in Driver combo box in Connect dialog window.

  5. #5
    Join Date
    Jun 2007
    Location
    Louisiana
    Posts
    77
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem with SqLite and Qt

    Thanks very much for the clarification. I can see now how it happened. I did change my code and it did eliminate the double definitions.

    However I still get a program stop due to some event in the shared library (unexplained by debug messages) on the addDatabase call.

  6. #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: Problem with SqLite and Qt

    Quote Originally Posted by wysota View Post
    It doesn't mean you can't guess in what part of code you are.
    Yes, you can guess. Guess is a very good word here.

    Quote Originally Posted by wysota View Post
    Provided you have time and manage to isolate the problem.
    So instead, you prefer to spend time on covering up the failure and trying to fix the errors it has caused using an unreliable mechanism, right? I prefer the fail-fast strategy.

    Quote Originally Posted by wysota View Post
    Now that I think of it... no
    Too bad that you have changed your mind. Again.

    Quote Originally Posted by wysota View Post
    I just remembered that when you use mprotect, access to a protected region of memory causes a segfault, but the app is perfectly fine, so you can easily recover and continue. There is a chance it's not a sole case.
    First you give an example, then you write about my example:

    Quote Originally Posted by wysota View Post
    Yes, or you might be running a system that doesn't handle signals or find dozens of other cases where it won't work.
    I see the pot is calling the kettle black.

  7. #7
    Join Date
    Jun 2007
    Location
    Louisiana
    Posts
    77
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem with SqLite and Qt

    Ok here is what I have

    /usr/lib/qt3(or qt4)/plugins/sqldrivers:

    libsqlite.so
    libsqlmysql.so
    libqsqlodbc.so
    libqsqlpsql.so

    This appears to be the shared library plugins for SQLite, MySQL, ODBC, and Postgress.

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

    Default Re: Problem with SqLite and Qt

    Quote Originally Posted by jacek View Post
    So instead, you prefer to spend time on covering up the failure and trying to fix the errors it has caused using an unreliable mechanism, right? I prefer the fail-fast strategy.
    If I have to perform some time intensive experiments and I'm short on time, so I know that I can barely make it with a non-crashing app, then I prefer it to crash now and then if I can continue the experiment without losing the already gathered data instead of debugging the application potentially for hours (remember the bug in the proxy model that caused you so much trouble?)

    Too bad that you have changed your mind. Again.
    I don't know what makes you think I changed my mind. I just remembered a real and useful example of trapping SIGSEGV where you can safely continue execution. If the app is broken, it's not safe to continue, but not every SIGSEGV means the app is broken.

    First you give an example, then you write about my example:
    I give you an example that there exists a possibility of using the approach and you counter with an argument that it might not always be possible to use it. I never said it was the UniversalWayOfDoingThings(TM).

    I see the pot is calling the kettle black.
    I'm afraid I don't know that phrase...


    Quote Originally Posted by ad5xj
    However I still get a program stop due to some event in the shared library (unexplained by debug messages) on the addDatabase call.
    That's perfectly normal when using a debugger. Just continue execution.

  9. #9
    Join Date
    Jun 2007
    Location
    Louisiana
    Posts
    77
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem with SqLite and Qt

    Well I did hit continue and gdb give me this:

    Failed to read a valid object file image from memory.

    (gdb) backtrace
    Stopped due to shared library event
    #0 0xb7f0ec70 in _dl_debug_state () from /lib/ld-linux.so.2
    #1 0xb7f036f6 in dl_main () from /lib/ld-linux.so.2
    #2 0xb7f13e6b in _dl_sysdep_start () from /lib/ld-linux.so.2
    #3 0xb7f012c4 in _dl_start () from /lib/ld-linux.so.2
    #4 0xb7f008d7 in _start () from /lib/ld-linux.so.2
    (gdb) info args
    No symbol table info available.
    (gdb) info local
    No symbol table info available.
    (gdb) frame 0
    #0 0xb7f0ec70 in _dl_debug_state () from /lib/ld-linux.so.2
    (gdb) info args
    No symbol table info available.
    (gdb) info local
    No symbol table info available.
    (gdb) continue
    [Thread debugging using libthread_db enabled]
    [New Thread -1221592864 (LWP 6190)]
    Stopped due to shared library event
    (gdb) continue
    [Switching to Thread -1221592864 (LWP 6190)]
    Stopped due to shared library event
    (gdb) continue
    Stopped due to shared library event
    (gdb) continue
    Stopped due to shared library event
    (gdb) continue
    Stopped due to shared library event
    (gdb) continue
    Stopped due to shared library event
    (gdb) continue
    Stopped due to shared library event
    (gdb) continue
    Stopped due to shared library event
    (gdb) continue

    Breakpoint 1, MainWindow::createConnection (this=0x80d9990) at mainwindow.ui.h:354
    /home/ken/NetLogger/mainwindow.ui.h:354:12111:beg:0x806100c
    (gdb) info thread
    * 1 Thread -1221592864 (LWP 6190) MainWindow::createConnection (this=0x80d9990) at mainwindow.ui.h:354
    (gdb) backtrace
    #0 MainWindow::createConnection (this=0x80d9990) at mainwindow.ui.h:354
    #1 0x08054c10 in MainWindow::init (this=0x80d9990) at mainwindow.ui.h:53
    #2 0x0805a7e0 in MainWindow (this=0x80d9990, parent=0x0, name=0x0, fl=1) at mainwindow.cpp:740
    #3 0x0804f660 in main (argc=
    Cannot access memory at address 0x0

    ) at main.cpp:21
    (gdb) info breakpoints
    Num Type Disp Enb Address What
    1 breakpoint keep y 0x0806100c in MainWindow::createConnection() at mainwindow.ui.h:354
    breakpoint already hit 1 time
    (gdb) info args
    this = (class MainWindow * const) 0x80d9990
    (gdb) info local
    No locals.
    (gdb) whatis this
    type = class MainWindow * const
    (gdb) continue
    Stopped due to shared library event
    (gdb) continue
    Stopped due to shared library event
    (gdb) continue

    Program received signal SIGSEGV, Segmentation fault.
    0x0806108a in MainWindow::createConnection (this=0x80d9990) at mainwindow.ui.h:355
    /home/ken/NetLogger/mainwindow.ui.h:355:12154:beg:0x806108a
    (gdb) info thread
    * 1 Thread -1221592864 (LWP 6190) 0x0806108a in MainWindow::createConnection (this=0x80d9990) at mainwindow.ui.h:355
    (gdb) backtrace
    #0 0x0806108a in MainWindow::createConnection (this=0x80d9990) at mainwindow.ui.h:355
    #1 0x08054c10 in MainWindow::init (this=0x80d9990) at mainwindow.ui.h:53
    #2 0x0805a7e0 in MainWindow (this=0x80d9990, parent=0x0, name=0x0, fl=1) at mainwindow.cpp:740
    #3 0x0804f660 in main (argc=
    Cannot access memory at address 0x0

    ) at main.cpp:21
    (gdb) info args
    this = (class MainWindow * const) 0x80d9990
    (gdb) info local
    No locals.
    (gdb) step
    Couldn't get registers: No such process.
    (gdb) info thread
    Couldn't get registers: No such process.
    (gdb) backtrace
    (gdb) Process exited

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

    Default Re: Problem with SqLite and Qt

    What exactly should we be looking at? I see you have a segfault What's inside mainwindow.ui.h:355?

  11. #11
    Join Date
    Jun 2007
    Location
    Louisiana
    Posts
    77
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem with SqLite and Qt

    A call to QSqlDatabase::setDatabasename()

  12. #12
    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: Problem with SqLite and Qt

    Quote Originally Posted by ad5xj View Post
    A call to QSqlDatabase::setDatabasename()
    Could you post the code of MainWindow::createConnection()?

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

    Default Re: Problem with SqLite and Qt

    Quote Originally Posted by jacek View Post
    I believe that a better approach is to save the data before the crash occurs, because you can't trust the memory contents after it.
    Yes, of course, provided it is possible. Dumping lots of data to disk (or even ramdisk) takes time. If you can do it in an incremental fashion, it's ok, but otherwise it will slow down the experiment even more. I know you like your apps to be perfect and bug free and I completely agree with you, but sometimes the practical side differs from the theory and it's nice to have a way that can save you in such cases.

    Yes, I do remember, even better than you. It had nothing to do with crashes and preserving critical data. Simply a few empty columns were visible in the QTableView.
    The point is you wasted a lot of time to find the problem. And the problem wasn't in your code, so fixing it wasn't that easy.

    So if you didn't change your mind, you still agree with me, right?
    Read my post again.

    We already agreed that in case of a SIGSEGV, you can't tell what caused it, so as you said, you can only guess.
    No, not in that case. Here you can be pretty certain what happened by tracking the state of the application and performing some checks. And even if you couldn't, if you test your application enough, you can assume the problem doesn't come from somewhere else. And even if you didn't, you can continue and if something is really broken, the segfault will occur again and you can abort the app then.

    Consider this snippet:
    Qt Code:
    1. void somehandler(int){ flag = TRUE; }
    2. //...
    3. signal(SIGSEGV, somehandler);
    4. protected[4] = 7; // sigsegv here
    5. signal(SIGSEGV, SIG_DFL);
    6. if(flag){ ... }
    To copy to clipboard, switch view to plain text mode 

    Here I can pretty well define where the segmentation fault occured and the example can be considered practical - it mimics a try-catch block.

    But earlier I've said that the stack might not be valid and you countered it with an argument that it might not always be a problem.
    But even earlier I said you can save the data and then you said the data might be on the stack and it might be broken. But I never said anything about saving the data from the stack, I was talking about saving data - some data.

    Don't you think you are a bit inconsistent in the way you treat arguments?
    I don't see any inconsistencies in my reasoning. I just see you can't admit that trapping SIGSEGV is not only a theoretical possibility. I have given you an example of such use and you still say the same. Maybe the snippet from this post will make you reconsider. Being stubborn in this case won't make your arguments better.

    Quote Originally Posted by jacek View Post
    Could you post the code of MainWindow::createConnection()?
    And the range of line numbers, please.

  14. #14
    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: Problem with SqLite and Qt

    Quote Originally Posted by wysota View Post
    Yes, of course, provided it is possible.
    Exactly the same thing can be said about your approach --- you can try to dump data after you intercept SIGSEGV, but it might not be possible. If I had to choose between possible decrease of performance and correct data, I would choose correct data.

    Quote Originally Posted by wysota View Post
    The point is you wasted a lot of time to find the problem. And the problem wasn't in your code, so fixing it wasn't that easy.
    But still it has nothing to do with the subject of this discussion, that is whether it is practical or not to recover from faults that caused SIGSEGV.

    Quote Originally Posted by wysota View Post
    you can continue and if something is really broken, the segfault will occur again and you can abort the app then.
    By that time your application could do something nasty. What if the SIGSEGV was caused by a buffer overflow? IMO fail-fast is much safer approach.

    Quote Originally Posted by wysota View Post
    Here I can pretty well define where the segmentation fault occured and the example can be considered practical - it mimics a try-catch block.
    I wouldn't call such abuse a practical example. Anyway, yes, you can provoke a segmentation fault and continue execution, but you are dragging this discussion away from where it started --- recovery from memory faults.

    Quote Originally Posted by wysota View Post
    But even earlier I said you can save the data and then you said the data might be on the stack and it might be broken. But I never said anything about saving the data from the stack, I was talking about saving data - some data.
    I'm afraid you failed to see the forest behind the trees. What I tried to show you is that you rebuke my arguments, yet at the same time you use exactly the same kind of argumentation to support your point. That's what I call an inconsistency.

    Quote Originally Posted by wysota View Post
    Being stubborn in this case won't make your arguments better.
    If you are going to step further into using ad hominem arguments, let's better simply agree that:
    1. my point is that it's impractical to recover from memory faults and continue execution after receiving SIGSEGV, because you can't be sure about the state of the memory and what caused the signal to be emitted,
    2. you don't agree with me.

  15. #15
    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: Problem with SqLite and Qt

    Quote Originally Posted by wysota View Post
    If I have to perform some time intensive experiments and I'm short on time, so I know that I can barely make it with a non-crashing app, then I prefer it to crash now and then if I can continue the experiment without losing the already gathered data
    I believe that a better approach is to save the data before the crash occurs, because you can't trust the memory contents after it.

    Quote Originally Posted by wysota View Post
    remember the bug in the proxy model that caused you so much trouble?
    Yes, I do remember, even better than you. It had nothing to do with crashes and preserving critical data. Simply a few empty columns were visible in the QTableView.

    Quote Originally Posted by wysota View Post
    I don't know what makes you think I changed my mind.
    So if you didn't change your mind, you still agree with me, right?

    Quote Originally Posted by wysota View Post
    If the app is broken, it's not safe to continue, but not every SIGSEGV means the app is broken.
    We already agreed that in case of a SIGSEGV, you can't tell what caused it, so as you said, you can only guess.

    Quote Originally Posted by wysota View Post
    I give you an example that there exists a possibility of using the approach and you counter with an argument that it might not always be possible to use it.
    But earlier I've said that the stack might not be valid and you countered it with an argument that it might not always be a problem.

    Don't you think you are a bit inconsistent in the way you treat arguments?

Similar Threads

  1. SQLite driver unavailable
    By kandalf in forum Installation and Deployment
    Replies: 5
    Last Post: 11th February 2009, 16:36
  2. Bulk insert into SQLite
    By munna in forum Qt Programming
    Replies: 6
    Last Post: 19th November 2007, 03:56
  3. SQLite make problems
    By raphaelf in forum Newbie
    Replies: 21
    Last Post: 3rd July 2007, 14:40
  4. SQLITE database problems
    By phoenix in forum Newbie
    Replies: 3
    Last Post: 30th April 2007, 21:38
  5. Reading umlaut letters from sqlite database
    By Djony in forum Qt Programming
    Replies: 11
    Last Post: 17th November 2006, 10: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.