Re: Program crashes when running it outside dev environment



z wrote:
I use Visual C 2005 to develop my programs. One in particular is
crashing in very specific and hard to replicate situations, made worse
by the fact it only crashes when run -outside- the dev - as an exe,
not from the Debug option. If I try to launch the debug on the
crashing program, it'll close before the debugger opens. Any
suggestions as to how I should proceed? Thanks in advance.

Does it fail when the debug version is run outside the environment, or is it the release version that fails?
If it is the release version, a possible suggestion is that you actually can debug the release version as well, but some things might be *very* strange in the debugger (like freshly assigned variables keeping the old value and so on because it is not stored to memory yet, understanding of assembler is a plus in those cases)

To pinpoint an error location outside of the environment could be possible doing either of those:
for GUI apps:
MessageBox(...);
MessageBeep(...);
for console apps:
printf(...); or fprintf(stderr,...);
(replace ... with proper code...) Of course the first two are very platform specific.

When all else fail I usually put a pair of those statements in suspected top level functions, one at the beginning and one at the end of each function like this:

void somefunction(void)
{
MessageBox("somefunction...","",MB_OK|MB_ICONINFORMATION);

/* the body of the function */

MessageBox("...somefunction","",MB_OK|MB_ICONINFORMATION);
}

This obviously won't work well for functions called lots of times unless they crash fairly fast in program execution.
When a function is pinpointed I put in more message boxes in that function in key places and so on...

For console apps fprintf(stderr,...) using the same basic technique could be used instead, I don't know if it would be a good idea to add a fflush(stderr) to each call... Someone else here could probably answer that if necessary.


If it just fails strangely and sometimes inside system functions it might be overwritten memory or some such problem. I use to put statements like this at key points in my program to find the real reason:
ASSERT(AfxCheckMemory());
(ASSERT only works in debug version however, if you want to do this in the release version you have to alter the code to some similar)
AfxCheckMemory() is probably very microsoft specific and not at all portable, but you don't want to keep them there once the problem is found anyway because they takes some time to execute.

Put one of those lines immediately before the line that crashes the program. If the ASSERT fails you have a memory corruption problem somewhere in your program.
If that is the case I put pairs of those in each top level suspected function. If it crashes on the top one the problem is elsewhere, if it crashes on the bottom one it is in that function...

If you have multiple threads also it might be a real nightmare to debug... Try to shut of parts not needed.
.



Relevant Pages

  • Strange debugger behavior
    ... A second window opens up as it crashes, and it's a second debug window with ... How can I tell where my program is crashing? ...
    (comp.sys.mac.programmer.codewarrior)
  • Re: Windows service crash
    ... Have you tried to build a debug version of your service and try to attach to it? ... The reason of crashing could be anything and anywhere. ... Even if you can attach it to the process and the crash happens randomly, it is still difficult to find out why it is crashing. ... I guess you will need to isolate where your program crashes, at managed code or unmanaged code level. ...
    (microsoft.public.dotnet.framework.clr)
  • Re: Program crashes when running it outside dev environment
    ... by the fact it only crashes when run -outside- the dev - as an exe, ... If I try to launch the debug on the ... crashing program, it'll close before the debugger opens. ...
    (comp.lang.c)
  • Re: thanks and question
    ... Warcraft, I get random crashes, my specs are: ... about it now, becuase im still on Safe Fail Defualts lol, Video Card, ... I *still* tend to suspect memory problems. ... Then, if it fails with one stick and not the other, you have your ...
    (alt.comp.hardware.pc-homebuilt)
  • Re: problem with release DLL.
    ... i tried by commenting certain suspicious statements in my code and found ... assigned variable or memory overflow problem. ... actually crashes, it is a stack problem where there is an array or character ... though it isn't alway the same as debug (optimization sometimes changes the ...
    (microsoft.public.vc.mfc)