Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

In c++ program after long run I am getting an error at memory i think. The error shows
Unhandled exception at [address] in [projectname].exe: 0xc0000005: Access violation.

Is there any way to handle this issue with try catch. This make my application to crash. Please help me on this thank you.

What I have tried:

Unhandled exception at [address] in [projectname].exe: 0xc0000005: Access violation.
Posted
Updated 20-Feb-20 16:54pm

You can catch such exceptions when enabling SEH (structured exception handling, see -EH (Exception Handling Model)[^]).

Because such exceptions occur asynchronous, they are fatal errors and the application should terminate. So catching such exceptions has no benfits for release builds besides generating your own error message or logging them.

But you can use them temporarily to get closer to the source of the problem by inserting try catch blocks at prominent code locations. Once you have a smaller portion of code that throws the exception, inspect it to find probable sources.

If not done so far, ensure that all pointer variables are initialised and perform assert() checks for debug builds.

To nail it down:
You have a bug in your code that must be fixed. It can be generally handled by catching the exception due to the asynchronism.
 
Share this answer
 
Comments
ssyuvaraja 15-Feb-18 5:26am    
Hi, Thank you. I try to find whether the exact location is causing the issue. This will be helpful, with log and check it. Its for development purpose to resolve the issue. Thank you so much.
[no name] 15-Feb-18 5:33am    
Uninitialised pointer problems I would expect to show up early in a run. The assert() is good advice and I wonder if stress testing in debug would show up something. My 5.
Jochen Arndt 15-Feb-18 9:28am    
Thank you.

When using asserts, pointer variables must be initialised with NULL and also cleared again when necessary (e.g. after releasing memory). Otherwise asserts are useless. Maybe that was not clear enough explained.
This is a memory access violation. Your program is accessing invalid memory. Normally a misleading pointer: deleted memory or the pointer is set or used wrongly.

You must debug the code line in which it happens. I know an option in Visual Studio to hit a debug point when memory or a value is changed, but not remember where it is now... (sorry)

Read the Using Breakpoints: Additional Information to find a way to set a helpful breakpoint and debug.
 
Share this answer
 
v2
Comments
ssyuvaraja 15-Feb-18 3:07am    
Hi, Thank you. yes its because of memory. Memory address for pointer working fine for more then a day. At long run on certain point the memory corrupted, that causing the issue. So I want to catch that issue. Is there any way to use try...catch for this to handle it.
Stefan_Lang 21-Feb-20 4:54am    
You could add code to test pointer values not for null, but for numbers below 1024. This is not 100% reliable, but when you have issues with memory allocation, the invalid pointers will be in that range most of the time. In any case, no valid pointer should be in that range, so you won't catch false positives.

P.s.: I've read that in some thread on SO some time ago. Unfortunately I can't find the link anymore, so I can't say what the source is for 1024. However, in UNIX, and probably some (most? all?) Linux distributions, addresses below 0x8000 are treated as seg fault. I have not found any such limit for Windows 10, but Win32 (without the /3gb switch set) used to reserve the address space below 0x10000.
KarstenK 16-Feb-18 3:45am    
Added a link. You must debug your app - there is no way around. Some memory leaks are minimal, stay undercover because of a bigger buffer or are only in some random code pathes and are so hard to detect.
You should also look into using the DbgHelp.DLL library (at least in debug versions). Given debug information and symbols, it can be used to create a stack trace starting at the point where the exception was raised.
 
Share this answer
 
unhandled exception 0xC0000005 at adress 0x01197ED8
 
Share this answer
 
Comments
Richard Deeming 21-Feb-20 7:58am    
Your error message is not a "solution" to someone else's question.

Read the existing solutions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900