Results 1 to 9 of 9

Thread: Release ok, debug crashes [compilable code]

  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

    Default Release ok, debug crashes [compilable code]

    Hi everyone,

    I'm here to provide you some fun
    Take this code and paste it in a QPushButton::clicked() slot
    Qt Code:
    1. mydb db1;
    2. if(db1.open())
    3. QMessageBox::information(this,"DEBUG info","Connected");
    4. db1.close();
    To copy to clipboard, switch view to plain text mode 
    then paste this somewhere in the .h or .cpp as you prefer
    Qt Code:
    1. class mydb{
    2. private:
    3. QVector<QSqlDatabase*> *handle;
    4. public:
    5. mydb(){ handle=new QVector<QSqlDatabase*>(); }
    6. ~mydb(){ delete handle; }
    7. bool open()
    8. {
    9. QSqlDatabase handle=QSqlDatabase::addDatabase("QMYSQL","connName");
    10. handle.setHostName("host");
    11. handle.setDatabaseName("db");
    12. handle.setUserName("username");
    13. handle.setPassword("password");
    14. handle.open();
    15. this->handle->append(&handle);
    16. return handle.isOpen();
    17. }
    18.  
    19. void close()
    20. {
    21. QSqlDatabase *h=handle->at(0);
    22. if(h->isValid())
    23. qDebug() << "Valid";
    24. if(h->isOpen())
    25. qDebug() << "Open";
    26. h->close();
    27. handle->remove(0);
    28. }
    29. };
    To copy to clipboard, switch view to plain text mode 
    includes
    Qt Code:
    1. #include <QtGui>
    2. #include <QtSql>
    3. // these are enhough, don't forget QT+=sql in file .pro
    To copy to clipboard, switch view to plain text mode 
    I'm compiling with mingw and running under Windows Xp SP3.
    To me this code runs clearly in release and raises exception in debug.
    Switching between modes with QtCreator causes it to change behavior.

    I see nothing wrong in code... maybe you can notice something I'm missing.

    Have fun
    Last edited by Raccoon29; 10th December 2009 at 14:51. 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
    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: Release ok, debug crashes [compilable code]

    Quote Originally Posted by Raccoon29 View Post
    I see nothing wrong in code...
    I do. You are assigning a pointer to a local object to a non-local variable thus your close() method is bound to crash the 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.


  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: Release ok, debug crashes [compilable code]

    Quote Originally Posted by wysota View Post
    I do. You are assigning a pointer to a local object to a non-local variable thus your close() method is bound to crash the application.
    Yes, but why it works -somehow- in release and crash in debug? An exception is an exception, isn't it?

    [OT]S.wysota you were promoted from Guru to Master of Zen, congratulations, you worth it all! [/OT]
    --
    raccoon29

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

  4. #4
    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: Release ok, debug crashes [compilable code]

    Quote Originally Posted by Raccoon29 View Post
    Yes, but why it works -somehow- in release and crash in debug? An exception is an exception, isn't it?
    It works because the debug version probably zeroes the stack memory it frees while release mode doesn't. So in the release mode the object is still there although formally it has been deallocated. In debug you either dereference a null object or the stack location is being overwritten by another data.
    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.


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

    Raccoon29 (10th December 2009)

  6. #5
    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: Release ok, debug crashes [compilable code]

    Quote Originally Posted by wysota View Post
    It works because the debug version probably zeroes the stack memory it frees while release mode doesn't.
    I see! So it becomes a sort of time bomb. Cute
    Anyway this QSqlDatabase in 'mydb' class is providing me with several headaches.
    What would be the correct way to write the above class? (which will hopefully close the thread)
    --
    raccoon29

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

  7. #6
    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: Release ok, debug crashes [compilable code]

    You can always fetch the connection handler by name using a proper static method from QSqlDatabase. So all you need to store (if at all) is the connection name. Remember they have to be unique.
    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. The following user says thank you to wysota for this useful post:

    Raccoon29 (16th December 2009)

  9. #7
    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: Release ok, debug crashes [compilable code]

    Quote Originally Posted by S.wysota View Post
    You can always fetch the connection handler by name using a proper static method from QSqlDatabase. So all you need to store (if at all) is the connection name. Remember they have to be unique.
    I read somewhere that this has performance influences; true? false? Who knows, anyway your solution seems to be the reason of everything.

    < You fixed it?? How in the world... Saint not for joke! >
    --
    raccoon29

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

  10. #8
    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: Release ok, debug crashes [compilable code]

    Quote Originally Posted by Raccoon29 View Post
    I read somewhere that this has performance influences; true? false?
    Ask the guy who wrote it
    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.


  11. #9
    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: Release ok, debug crashes [compilable code]

    Quote Originally Posted by S.wysota View Post
    Ask the guy who wrote it
    Smart
    --
    raccoon29

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

Similar Threads

  1. Replies: 3
    Last Post: 3rd December 2009, 16:12
  2. Replies: 1
    Last Post: 18th November 2009, 21:51
  3. Replies: 5
    Last Post: 5th October 2008, 05:12
  4. Need debug and release versions of Qwt?
    By Tiansen in forum Qwt
    Replies: 1
    Last Post: 28th March 2008, 07:55
  5. Replies: 6
    Last Post: 10th November 2006, 10:38

Tags for this Thread

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.