Link to home
Start Free TrialLog in
Avatar of Dannen
Dannen

asked on

Ignore Unhanlded Exceptions

I wrote a small Win32 App that downloads data from the internet and writes it to a file... everything works fine, except the program randomly crashes after the 6th or 10th loop.
Everytime the program crashes it does in diferent parts of the code. But 2 are the parts where it crashes the most.
in a memory allocation
databuff = new char [11];

I've never programmed with exceptions before... So I did a small test with this code:

try
{
      databuff = new char [11];
}
catch(bad_alloc& ba)
{
      return fail; // returns a fail code, recognized by the caller
}
And If it crashes in the allocation it says: Unhandled Exception

and in a ofstream operation
myfile << databuff;

The crash most of the time says:
Unhandled Exception at 0x7c93426d in myapp.exe: 0xC0000005: access violation reading 0x00000000.

I know the above error is because reading a null pointer but, think of something like this:
char* databuff=0;
databuff = DownloadData();
if (verifications that downloaded data is real data)
{
myfile << databuff;
}
It shouldn't attemp to write the databuff is it's null!

I've traced that variable with the debugger and it never gets null, just when I run the app outside the debugger it's when it crahes.

As this app needs to be done soon there's no problem if it crashes... Only if does crash silently.
There's another mechanish that will restart the app if it crashes.

So... My question is, How can I setup my code so I can exit my app silently if a exception is found?


SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium 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
ASKER CERTIFIED 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
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
Avatar of Dannen
Dannen

ASKER

>>To silently exit your program when an error like this occurs, you could simply call
>>SetErrorMode (SEM_FAILCRITICALERRORS);
It still displays the message Unhanlded Exception

>>That sounds a lot like heap corruption to me.
Will try locate the error, it I can't in a few hours I'll post more code.
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
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
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
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
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
Avatar of Dannen

ASKER

It was heap corruption. Im gonna make some extensive test.
If no further problems are found I'm gonna close this question