PDA

View Full Version : a strange crash



hxf@dlut.edu.cn
7th March 2016, 15:48
Basic information:
Qt4.8.6 + mingw + win8 + gdb + qtcreator
problem description:
The application program compiles ok in both debug and release modes, but crashes randomly when I run it. I tried all my best to find if there is a bug in my code, but the code looks ok. The strange thing is that when I run the same program in the step-by-step debugger, it never crash. I noticed that when the debugger runs it reports "load ...*dll successful", so I guess if some library *dll can be loaded by the debugger but can not be loaded when I actually run it by click the green button "run"?

anda_skoa
7th March 2016, 16:15
You need to do a post-mortem analysis, i.e. look at the stack trace when it crashed.

Maybe you are accessing an uninitialized pointer or a pointer to a deleted object, etc.

Cheers,
_

hxf@dlut.edu.cn
7th March 2016, 22:56
Thanks for reply, anda_skoa. In fact what you said is a good way to solve the problem, however, the program runs well in the debugger mode. Or in other words, when I use the debugger mode(GDB) to figure out the location where it crashes, the crash never happens. Or, maybe do you know another way to find the stack trace except by using GDB?

anda_skoa
8th March 2016, 10:00
You don't need to run it in the debugger, when it crashed it should still be debuggable.
I think on Windows you get a dialog asking if you want to Retry and that starts the debugger or maybe that is compiler specific and a MinGW built program also creates a core dump like on other operating systems.

Cheers,
_

hxf@dlut.edu.cn
8th March 2016, 13:01
[solved] Cheers, my problem has been solved, at least, it looks so currently :)
What I would like to share including:
(1). the reason caused program crash was that I used an uninitialized variable (int max_value) as the bound in a loop:

for(int i=0; i<max_value; i++){a[i]=***;} //int a[10] is a arrary
so the above loop statement might lead to an illegal usage of memory especially when the value of "max_value" initialized by windows 8 system is very big!
I found the value of max_value initialized by windows 8 is 14339584, and lead to the crash as a result.

(2). On how to debug. I tried to figure out the problem in the Qt debugger mode (GDB) but the program never crash as I said at the beginning. Maybe in the environment set by the debugger gave a reasonable value to "max_value", I didn't check. My method which helped me figure out the problem successfully here was the oldest one: print everywhere. Of course, it is unnecessary to print everywhere if you know you code very well, just print at some key points in the flow chart of your code. Find the possible codes where the bug is then continue to print in that area. Honestly, it took me only 2 minutes to find out and fix the bug which confused me at least for 2 whole working days. Programmers should calm down and think.

(3). Another interesting thing is that when the program crash Qt reported: "quit with code -1073741819", and I found many people were asking and discussing on the "-1073741819 code". Maybe what I posted here helps.

(4). [ask]. I still want to know on how to generate a core dump like file in windows and how to use it to debug? should gdb still work? Anyway, powerful debug tools are still desired to find the location quickly!


You don't need to run it in the debugger, when it crashed it should still be debuggable.
I think on Windows you get a dialog asking if you want to Retry and that starts the debugger or maybe that is compiler specific and a MinGW built program also creates a core dump like on other operating systems.

Cheers,
_

anda_skoa
8th March 2016, 13:34
I found the value of max_value initialized by windows 8 is 14339584, and lead to the crash as a result.

An uninitialized variable is, by definition, uninitialized :)
It can have any value, basically whatever the content of the memory at its location is at the time of access.

Modern compilers and static code analyzers and memory checkers can detect and warn these things.

Better of course is to always initialize variables.



(4). [ask]. I still want to know on how to generate a core dump like file in windows and how to use it to debug? should gdb still work?
Someone developing on Windows will have to answer that but this is how it work on all other platforms.

Cheers,
_

jefftee
9th March 2016, 04:20
I still want to know on how to generate a core dump like file in windows and how to use it to debug? should gdb still work? Anyway, powerful debug tools are still desired to find the location quickly!
Many years since I have done any debugging on windows, but I suspect it hasn't changed much. Google windbg and symbols. Basically MS provides symbols for all of the windows run-time dll's and you can generate symbols for your own app executables using MS tool chain.

Hope that gets you started in the right direction. Good luck!

P.S. Sorry, I just noticed that you are using mingw tool chain, not sure if it and/or GDB are windows symbols aware, etc. You'll have to google around to find out whether or not that's the case or whether windbg/symbols approach will be usable if not using the MS tool chain.

hxf@dlut.edu.cn
9th March 2016, 07:49
Thank you for your guides, Jeff, if you don't mind I call you Jeff. Actually my first Qt program and maybe my first c++ program as well were coded on Linux red hat. At that time I have good colleagues to set all the debug things and all the environment things as well. Then I quit that place where I worked for nearly two years, and moved to a new country, and I have to do things all by myself. Got myself crazy on some difficult/strange issues. Your name/ID Jeff makes me so said because at that time Jeff and I shared an office. He was always staring on the computer screen, and I think he is still there, maybe still working on that computer this afternoon. Next time I just send my problem to Jeff directly, maybe that would help. :) :) :)


Many years since I have done any debugging on windows, but I suspect it hasn't changed much. Google windbg and symbols. Basically MS provides symbols for all of the windows run-time dll's and you can generate symbols for your own app executables using MS tool chain.

Hope that gets you started in the right direction. Good luck!

P.S. Sorry, I just noticed that you are using mingw tool chain, not sure if it and/or GDB are windows symbols aware, etc. You'll have to google around to find out whether or not that's the case or whether windbg/symbols approach will be usable if not using the MS tool chain.