Results 1 to 10 of 10

Thread: Writing to statusBar causes bug in my application

  1. #1
    Join Date
    Nov 2011
    Posts
    51
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Writing to statusBar causes bug in my application

    Hi, I have a very strange bug involving the statusBar. With this line:

    Qt Code:
    1. ui->statusBar->showMessage("Pressure deviation");
    To copy to clipboard, switch view to plain text mode 

    I manage to screw my program up badly. The bug appears in a completely other part of the program which doesn't have anything to do with Qt (in fact that part is written as an API), and I never read from the statusBar so I don't know ho writing to it can cause the bug to appear. If I comment the line out the bug will never appear.

    I would appreciate any help I could get to solve this problem. Thanks in advance.

  2. #2
    Join Date
    Sep 2011
    Posts
    1,245
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Writing to statusBar causes bug in my application

    and when you debugged this and looked at the value for ui->statusBar, what did you see? You have debugged this, haven't you?
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Nov 2011
    Posts
    51
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Writing to statusBar causes bug in my application

    Hi amleto and thanks for your answer. I can't see the value for statusBar in the debugger, since the ui object can't be expanded for some reason... If I want to print the text that the statusBar displays to stdout, which property is it I shall print in that case? Otherwise I don't really know how I can debug this, since I don't really know how statusBar works and what it is that can have gone wrong.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,376
    Thanks
    246
    Thanked 687 Times in 677 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Writing to statusBar causes bug in my application

    I can't see the value for statusBar in the debugger, since the ui object can't be expanded for some reason...
    Then that tells me that the memory occupied by "ui" is already corrupted before you ever get to the part where you try to set the status message. The error you are seeing has nothing to do with the status bar, that's just a red herring. The real problem is a memory corruption error.

    - Make sure that all of your pointers are initialized before you try to use them
    - Make sure all other variables are initialized, either to zero or some non-valid value.
    - Remember that the debugger sets uninitialized variables to some non-valid value so they will be easily recognized when debugging (like 0xdcdcdcdc or whatever) and resets the values of pointers to another bogus value when they have been deleted (but are still in scope). If you see things like that, there's your memory problem.
    - Also remember that this doesn't happen in Release mode - variables and pointers do not get initialized, so they hold whatever value the memory location they are in had when they come into scope. Behavior in Debug and Release modes can therefore be different.

  5. #5
    Join Date
    Sep 2011
    Posts
    1,245
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Writing to statusBar causes bug in my application

    I doubt it's memory corruption, more like simple un-initialisation error. pebkac
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  6. #6
    Join Date
    Nov 2011
    Posts
    51
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Writing to statusBar causes bug in my application

    How could un-initialization cause this behavior? I can also mention that the writing to the statusBar is activated by an event triggered by the user, and if the user doesn't trigger the event so that nothing is written to the statusBar, the bug does not appear either. I think memory corruption is more likely; any way to detect where the memory gets corrupted?

  7. #7
    Join Date
    Sep 2011
    Posts
    1,245
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Writing to statusBar causes bug in my application

    int* p;
    *p; <--- bang!

    You deref an uninitialised pointer and you get a crash. Did you use your debugger yet? You haven't mentioned anything, and it should be your first point of call**. You should be looking for horses before you blame zebras...

    **
    edit: oh yes, you said you can't expand it. Didn't that seem strange to you? I wonder why it's not working? Probably because it hasn't been initialised...
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  8. #8
    Join Date
    Nov 2011
    Posts
    51
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Writing to statusBar causes bug in my application

    Quote Originally Posted by amleto View Post
    int* p;
    *p; <--- bang!
    Ah, okay this is what you meant with un-initialized. Yes that is very likely. I thought this was what d_stranz was referring to when he mentioned memory corruption, since an un-initialized pointer easily can lead to memory corruption (I guess), and I thought you were referring to an uninitialized float (since the bug was that I got a very strange value for a float, although I didn't mention it).

    Quote Originally Posted by amleto View Post
    Did you use your debugger yet?
    No, I'm sorry I haven't had the opportunity yet, so maybe it's stupid of me to ask more questions. But I am going to do it soon and get back to this thread when I have found the reason for the bug.

    Quote Originally Posted by amleto View Post
    edit: oh yes, you said you can't expand it. Didn't that seem strange to you? I wonder why it's not working? Probably because it hasn't been initialised...
    Do you mean that the ui object hasn't been initialized? The ui object must have been initialized, since my main window constructor definition starts like this:

    Qt Code:
    1. mainwin::mainwin(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::mainwin)
    4. {
    To copy to clipboard, switch view to plain text mode 

    and that's the constructor I use when I'm creating the mainwin object, so the ui object has been initialized with an initializer.

    I'm thinking that the ui object has been corrupted, and that I have some uninitialized pointer somewhere that I'm using, like you said. But how can I find places where I'm dereferring uninitialized pointers?

  9. #9
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Writing to statusBar causes bug in my application

    What about objects inside ui?
    Are you calling ui->setupUi(this)?

    I'm guessing you are, otherwise you wouldn't see anything, but I may be wrong.

  10. #10
    Join Date
    Sep 2011
    Posts
    1,245
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Writing to statusBar causes bug in my application

    Quote Originally Posted by Yes View Post
    But how can I find places where I'm dereferring uninitialized pointers?
    attach your debugger when your program crashes / look at the call stack when debugging and the error occurs.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. Writing to StatusBar
    By snowsoman in forum Qt Programming
    Replies: 1
    Last Post: 23rd November 2011, 16:50
  2. Replies: 2
    Last Post: 26th February 2011, 15:25
  3. Writing a Qt console application
    By sashoalm in forum Newbie
    Replies: 6
    Last Post: 7th February 2011, 17:42
  4. Replies: 1
    Last Post: 20th February 2010, 04:26
  5. statusbar
    By bhogasena in forum Qt Programming
    Replies: 7
    Last Post: 3rd February 2009, 14:54

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.