Results 1 to 19 of 19

Thread: Simple code causes segmentation fault...in debug

  1. #1
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Exclamation Simple code causes segmentation fault...in debug

    Hello everyone,

    following code seems trivial to me:
    Qt Code:
    1. QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL","connection");
    2. db.setHostName("localhost");
    3. db.setDatabaseName("dbname");
    4. db.setUserName("user");
    5. db.setPassword("secret");
    6. db.open();
    7.  
    8. QSqlQuery query(db);
    9. query.prepare("SELECT col FROM table");
    10. query.exec();
    11. query.next();
    12.  
    13. db.close();
    14. QMessageBox::information(this,"DEBUG","Done");
    To copy to clipboard, switch view to plain text mode 
    anyway if executed in debug, it makes the application raise a segmenetation fault when closed, while in release everything works fine.

    If you destroy explictly the QSqlQuery object (allocating it dinamically or closing its code in { } braces if a stack object), everything works fine in debug too. Conflict?

    Try it yourself, behaves in the same way to you?

    Environment:
    OS: windows Xp SP3
    Qt: 4.3.0
    QtCreator: 1.1.1
    driver: libmysql4.dll / libmysqld4.dll
    patient: full
    luck: n/d
    ideas: over

    My issue? Qt issue? Driver issue? Destiny issue?

    Thanks in advance right to you reading!
    Last edited by Raccoon29; 16th December 2009 at 15:23. Reason: reformatted to look better
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  2. #2
    Join Date
    Feb 2009
    Location
    libya
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Simple code causes segmentation fault...in debug

    must be prepare mysql driver
    the mysql not default driver,

    see http://doc.trolltech.com/4.5/sql-driver.html

  3. #3
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Simple code causes segmentation fault...in debug

    Quote Originally Posted by gmaa45 View Post
    must be prepare mysql driver
    the mysql not default driver,

    see http://doc.trolltech.com/4.5/sql-driver.html
    Sorry, I don't get it.
    What do you mean? If you meant to tell me how to build the driver, that's not needed: I built it already and is able to query the database.
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  4. #4
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Simple code causes segmentation fault...in debug

    and where did you add those braces { } which make this code work?
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  5. #5
    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: Simple code causes segmentation fault...in debug

    I'd say it's a normal thing to get a warning (but not a segfault). If QSqlQuery object is still alive while the database connection is removed, Qt complains about it. But should definitely not crash.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Simple code causes segmentation fault...in debug

    Quote Originally Posted by faldżip View Post
    and where did you add those braces { } which make this code work?
    like this:
    Qt Code:
    1. QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL","connection");
    2. db.setHostName("localhost");
    3. db.setDatabaseName("dbname");
    4. db.setUserName("user");
    5. db.setPassword("secret");
    6. db.open();
    7.  
    8. {
    9. QSqlQuery query(db);
    10. query.prepare("SELECT col FROM table");
    11. query.exec();
    12. query.next();
    13. }
    14.  
    15. db.close();
    16. QMessageBox::information(this,"DEBUG","Done");
    To copy to clipboard, switch view to plain text mode 
    Quote Originally Posted by S.wysota
    I'd say it's a normal thing to get a warning (but not a segfault). If QSqlQuery object is still alive while the database connection is removed, Qt complains about it. But should definitely not crash.
    So I guess to you this code works?

    By the way, when segfaults output complains:
    HEAP[temp.exe]:
    HEAP: Free Heap block 52b3e88 modified at 52b3ee4 after it was freed
    and stack-trace shows:
    0 ntdll!DbgUiConnectToDbg C:\\WINDOWS\\system32\tdll.dll 0 1 ntdll!RtlpNtMakeTemporaryKey C:\\WINDOWS\\system32\tdll.dll 0
    2 ntdll!LdrFindEntryForAddress C:\\WINDOWS\\system32\tdll.dll 0
    3 ?? 0
    4 ?? 0
    5 ?? 0
    6 ?? 0
    mumble mumble...
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  7. #7
    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: Simple code causes segmentation fault...in debug

    The code is too generic to try it, but it should work. Could you try running this program to see if it crashes too?

    Qt Code:
    1. #include <QtDebug>
    2. int main(){
    3. qDebug() << "XXX";
    4. return 0;
    5. }
    To copy to clipboard, switch view to plain text mode 

    It seems you are having a problem with debugging, not with your application.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Simple code causes segmentation fault...in debug

    Quote Originally Posted by S.wysota View Post
    The code is too generic to try it, but it should work.
    Well, this is all the application is made of, after a lot of debug reduction, I got this simple code that make blow up everything.

    Quote Originally Posted by S.wysota View Post
    Could you try running this program to see if it crashes too?
    No, it doesn't (neither outputs anything).

    Quote Originally Posted by S.wysota View Post
    It seems you are having a problem with debugging, not with your application.
    But sounds strange that that code is required to have problems...isn't it?
    Just an idea: could it be the mysql driver somehow? (corruption and so on)
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  9. #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: Simple code causes segmentation fault...in debug

    It seems that's a windows dll that's failing. But the question is what is causing it. As for my test app, add CONFIG+=console to the project.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    Raccoon29 (17th December 2009)

  11. #10
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Simple code causes segmentation fault...in debug

    Quote Originally Posted by S.wysota View Post
    It seems that's a windows dll that's failing.
    I see: my system is slowly and painfully dieing...
    Quote Originally Posted by S.wysota
    But the question is what is causing it.
    I'm sweating here... if you get any clue or test about this, I'll try.

    Meanwhile thank you for all
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  12. #11
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Simple code causes segmentation fault...in debug

    UPDATE:

    I found another analog situation:
    Qt Code:
    1. QSqlQuery query(db->Handle()); //Handle() returns QSqlDatabase preset
    2. if(db->Connect()) //connection of same object of Handle()
    3. {
    4. query.prepare("SELECT something FROM somewhere"); //segfault!
    5. ...
    6. }
    To copy to clipboard, switch view to plain text mode 
    In short, in this situation it segfaults because I create QSqlQuery object before the connection is opened.
    Ok, that's bad, also documentation says that. But isn't segmentation fault a bit excessive??
    I mean, a warning within an undefined behavior would be enhough, and probably it is in a normal environment...

    Further more, trying to query a "USE db" command messes up everything, explosions everywhere.

    I fear that here is something heavy happening... I would like to use a "sure-working" mysql driver to know at least if that's the reason or not, but where to take it?
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  13. #12
    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: Simple code causes segmentation fault...in debug

    I'm pretty convinced this is not related to the database but to the fact that your program crashes at a moment when Qt tries to issue a warning to the console.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #13
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Simple code causes segmentation fault...in debug

    In release I went ahead and program works, but this debug not working...
    I compiled with QT += warn_off but I got the same problem: is this a relevant test or I missed the point?
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  15. #14
    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: Simple code causes segmentation fault...in debug

    You missed the point
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. #15
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Simple code causes segmentation fault...in debug

    Owned, lol

    I'm just ignoring debug since release works, if in the future this problem shall show up again... I'll invent something.

    Thank youuuuu

    EDIT: not QT+=warn_off, but CONFIG+=qt debug warn_off
    Last edited by Raccoon29; 18th December 2009 at 09:14.
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  17. #16
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Smile Re: Simple code causes segmentation fault...in debug

    I got it to work and I found the reason of everything.

    Solution was in documentation:
    Warning: You must load the SQL driver and open the connection before a QSqlQuery is created. Also, the connection must remain open while the query exists; otherwise, the behavior of QSqlQuery is undefined.
    in fact I was closing the connection while the related QSqlQuery was still alive, thus "undefined behavior" -> exception; this explains the { } braces trick too.

    Eventually somehow the exception was related to mysql driver, because the same code works with sqlite driver, maybe some different internal management, who knows...

    Anyway the problem is solved, thanks to everyone and first of all to S.wysota!
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  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: Simple code causes segmentation fault...in debug

    Quote Originally Posted by Raccoon29 View Post
    in fact I was closing the connection while the related QSqlQuery was still alive, thus "undefined behavior" -> exception; this explains the { } braces trick too.
    But you should get a warning and not a crash. Unless of course you are trying to actually access the query later on.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  19. #18
    Join Date
    Dec 2009
    Location
    Brisbane
    Posts
    17
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Simple code causes segmentation fault...in debug

    The problem is that mysql doesn't check pointers before dereferencing them (which is what those handles are). The code is cleaned up for 4.6, but yeah, if you do out of the ordinary things, then out of the ordinary things are bound to happen (in mysql's case, segfault :/)

  20. #19
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Simple code causes segmentation fault...in debug

    Quote Originally Posted by S.wysota View Post
    But you should get a warning and not a crash.
    As you suggested, maybe actually a warning that pops up in a bad moment makes it crash, this had to happen just in debug, in release no problem.
    Quote Originally Posted by tangential
    if you do out of the ordinary things, then out of the ordinary things are bound to happen (in mysql's case, segfault :/)
    ...and out of ordinary headhaches are provided.
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

Similar Threads

  1. problem with linking
    By mickey in forum Qt Programming
    Replies: 49
    Last Post: 12th August 2006, 21:41
  2. segmentation fault insert QString in QCombobox
    By regix in forum Qt Programming
    Replies: 16
    Last Post: 8th August 2006, 08:46
  3. (Another) segmentation fault
    By Lebowski in forum Qt Programming
    Replies: 27
    Last Post: 6th April 2006, 06:33
  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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.