PDA

View Full Version : Writing to statusBar causes bug in my application



Yes
18th May 2012, 12:15
Hi, I have a very strange bug involving the statusBar. With this line:


ui->statusBar->showMessage("Pressure deviation");

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.

amleto
18th May 2012, 13:32
and when you debugged this and looked at the value for ui->statusBar, what did you see? You have debugged this, haven't you?

Yes
18th May 2012, 15:31
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.

d_stranz
18th May 2012, 15:54
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.

amleto
18th May 2012, 18:48
I doubt it's memory corruption, more like simple un-initialisation error. pebkac :)

Yes
21st May 2012, 13:13
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?

amleto
21st May 2012, 19:07
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...

Yes
21st May 2012, 20:46
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).


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.


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:



mainwin::mainwin(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::mainwin)
{


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?

Spitfire
22nd May 2012, 12:27
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.

amleto
22nd May 2012, 12:56
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.