views:

147

answers:

7

Hey there!

I have an application that runs just fine in the debug build, but when I start it in the release build, i get a

unhandled Exception at 0x0043b134 in myapp.exe: 0xC0000005:
Access violation while reading at position 0x004bd96c

if i click on 'break' it tells me that there are no symboles loaded and the sourcecode can't be displayed.

What can I do in such a situation to track down the problem?

thanks!

+3  A: 

It could be two things:

  • One or more of your assertions does necessary work apart from the check itself
  • Something else

To rule out the former, try redefining assert as an empty operation in the debug build. If the absence of some assertion causes the crash, you will see it. Otherwise, it's something else.

Also, I assume you have version control. Did this just start happening? You can analyze the code changes from last week.

Finally, even in the absence of a crash in debug mode, running a memory checker tool may be useful to spot incorrect memory access.

Daniel Daranas
+1 for the version control suggestion.
Zooba
I use versioning control for all my projects except for that one since this particular machine i'm developing on is not connected to the internet :/ silly me... i redefined assert to do nothing and it does not crash in debug build. i'll google 'memory checker tool' - can you recommend one?
Mat
@Mat: Consider also @Grozz's suggestion of using log output. This will certainly put some light into it.
Daniel Daranas
@Mat: And about memory checking, see http://stackoverflow.com/questions/1010106/how-to-debug-heap-corruption-errors, http://stackoverflow.com/questions/167199/what-c-c-tools-can-check-for-buffer-overflows, http://stackoverflow.com/questions/638090/profiling-c-multi-threaded-applications and similar questions.
Daniel Daranas
+4  A: 

This kind of problem is often due to unitialized variables. I'd start there looking for your problem.

Debug mode is more forgiving because it is often configured to initialize variables that have not been explicitly initialized.

Perhaps you're deleting an unitialized pointer. In debug mode it works because pointer was nulled and delete ptr will be ok on NULL. On release it's some rubbish, then delete ptr will actually cause a problem.

Montdidier
that sounds reasonable! can I tell Visual Studio not to initialize variables in debug build?
Mat
I activated /RTCu (non initialized variables) in my debug build - shouldn't this nag if there were any uninitialized variables?
Mat
Montdidier
+1  A: 

Track down the problem inserting log output here and there right from the beginning of the main function.

Grozz
A: 

If it is not a memory issue, then you have to enable asserts in the release. For memory issues, I hope you got good unit tests. You can easily catch such problems with valgrind.

btw why are people disabling asserts in the release version? In 99% cases they do not cause performance problems, and are good in detecting errors.

VJo
A: 

Take a crash dump using Microsoft debugdiag on windows(it's free) and analyze the dump using the same. It gives a nice call stack for the function where it crashes. Although, if it keeps crashing all over the place, it could be an issue of heap corruption. You then need to use global flags(or gflags which is a part of microsoft tools for debugging suite which is free) in conjunction with debugdiag. Gflags would give you the location where the heap is actually getting corrupted. Hope that helps.

Samrat Patil
A: 

Without looking at the code, it is hard to tell what is bad. All the above suggestions are good and helpful but what I have found most helpful fixing this kind of things is to run certain parts of program in chunks. i.e., comment out a lot of code/functionality and then run the program and see if it crashes. If it doesn't, the uncomment some functionality and then re-run again and so on. This way you will be able to narrow down the problem to the exact code that is causing this.

In most of the cases this happens due to some Buffer overruns which Debug builds can guard against.

Aamir
+2  A: 

Two steps:

a) Build release build with debug symbols (possible with VS at least)

b) Build release build without optimization

If the problem still happens, it is very good and easy to fix. It is almsot as if the problem is in debug build.

If the problem happens with optimization settings on, then it is really tough and has to be handled in situation specific manner.

Chubsdad
if I build the release with debug symbols, it doesn't crash
Mat
@Mat: that is _very_ weird, as the binary should (for all purposes) be identical. The main difference is that the "debug symbols" option creates a separate .PDB file describing the relation between memory addresses and functions/variables. I.e. that should tell you what was at the address `0x0043b134` or the address `0x004bd96c`.
MSalters
@MSalters: I think the reason is that release binaries are always built with some optimization (/O2 e.g.). I suspect it has got to do with optimization flags.
Chubsdad
@Chubsdad: the claim here is that a "normal" release build crashes, but not a release build with symbols. That is weird, becuase both builds should have the same optimization flags etc. However, I did just notice that `/DEBUG` implies `/OPT:NOREF, /OPT:NOICF`; the documentation states that you need to set them explicitly for a release build with debug information. Those two flags would be my prime suspects now.
MSalters