Link to home
Start Free TrialLog in
Avatar of ProjectZIG
ProjectZIG

asked on

How can I figure out why my C++ program is crashing in Windows?

I have a program that I've written using Ultimate++

Normally, if I compile in debug mode and run in the debugger, then if an error or exception occurs it will jump me to that point in the file and tell me what line and what problem, which is extremely effective.

However...

Recently, it's been crashing and the error that it gives me isn't traceable back in code. It simply says "Read error at 0xHEXHEXHEX" (paraphrasing of course) and the debugger will not jump me to a line.

It's a multi-threaded application, and each thread has access to a global data array to pass information from one thread to the next. So, there could be a case of one writing and one reading to the same place at the same time which is what I assume is happening.

Since it won't tell me what line, or what error etc then I have no way to fix it.  Is there something else that I can do, debugging wise, to figure out where my program is crashing? If I figure out what line it is, then I'm sure I can do something to fix it.

My first thought was to write something that, on every line of code, it saves the line number to a file that way when it crashes I'll know what line it crashed on..but I would assume that this would be way too HDD or processor intensive.  What else could I possibly do?? I'm pretty frustrated here, so top points to absolutely anybody who can help me through this.

-Kevin
Avatar of Daniel Van Der Werken
Daniel Van Der Werken
Flag of United States of America image

So, go here:
http://msdn.microsoft.com/en-us/windows/hardware/gg463009
and read and install WinDBG and get the web symbols setup, and run the application with the debugger attached to the process and see if you can get into what is going on with the issue.
>>  read and install WinDBG and get the web symbols setup
Unlikely to make any difference because the most likely cause is stack corruption due to a race condition reading/writing to globals (hence, the call stack is shagged).

If you have multiple threads reading/writing to the same global variable without using synchronisation primitives to serialise access you will end up with a race condition (ie. undefined behaviour). Your first job should be to put a mutex or critical section around the code that changes these globals to remove any race conditions.
Avatar of ProjectZIG
ProjectZIG

ASKER

evilrix, that is exactly right.  See I had this problem before with one of the globals and fixed it by making it to where it writes to a DIFFERENT global first. Then, when I get to the place where it needs to read/write to the main global, I simply copy everything from the secondary one, run my checks,and then update the main one which seemed to help...though this may still be causing the same problem considering at the very beginning of the function it will still be reading from that location..so it may have slowed it down but not stopped it completely.

Can you elaborate a bit further as to the "synchronisation primitives to serialise access"? Also, a mutex or critical section..can you explain this further? they sound vaugely familiar but I'm not sure where to begin there other than doing some Googling, I figure you could explain it a bit better than Google can for my specific problem.

Thanks in advance!

ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i see that evilrix also has answered in the meantime. so take my code as an add-on ...

Sara
Ok, this really helps alot! I was trying to use flags to figure this out in the form of a global variable integer..but it gets real complicated.

So a question -- does the critical section thing..is this specific for each variable I need to protect? For example, I have about 7 global variable arrays that can be accessed at any given time. Do I need to set up critical sections for each like so?:

CRITICAL_SECTION cs1;
CRITICAL_SECTION cs2;
CRITICAL_SECTION cs3;
CRITICAL_SECTION cs4;
CRITICAL_SECTION cs5;

and then initialize each one when the program starts and use EnterCriticalSection(cs1/cs2/cs3/etc) or should I just use one of them and have them all run on the same lock?

-Kevin

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.