Results 1 to 19 of 19

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

Hybrid View

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

    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
    Qt products
    Qt3 Qt4
    Platforms
    Windows
    Thanks
    54
    Thanked 2 Times in 2 Posts

    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
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    4
    Thanked 140 Times in 132 Posts

    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
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Qt products
    Qt3 Qt4
    Platforms
    Windows
    Thanks
    54
    Thanked 2 Times in 2 Posts

    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 "

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    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.


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

    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 "

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    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.


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

    Raccoon29 (17th December 2009)

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

    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 "

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

    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 "

  12. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    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.


  13. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    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.


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
  •  
Qt is a trademark of The Qt Company.