Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have c++ windows application running as service on windows server 2003. We are experiencing increase in memory usage for this service & only solution seems to restart the service. we have seen occasional Win32 Structured Exception: 'ACCESS_VIOLATION in the application log. Can this be a cause? is there any tool to find out what causes the leak?
Posted
Comments
pasztorpisti 28-Sep-13 14:55pm    
The only valid answer is: debug it. Noone will find out whats wrong with your code and making assumptions like "Maybe because of ACCESS_VIOLATION..." is a pointless waste of time. Try to find out the location of the leak and then the cause. If you don't know how to do that then search for a memory leak debugging tutorial. Visual C++ has built in support for this. To debug the app in a real life scenario build it in debug, start it as a service, attach to it with the debugger and then reproduce the actions you suspect to be the cause of the leak.
chait301 28-Sep-13 18:54pm    
I guess i have not make my question clear.
Can Access Violation cause leak? If So how?
pasztorpisti 29-Sep-13 8:52am    
An unhandled (and even a handled) exception in C++ (and in most other non-garbage collected languages) can be extremely dangerous. Maybe this is one of the reasons why some C++ projects avoid using exceptions as an error handling mechanism. An unhandled exception basically returns from many function calls until someone handles it unwinding the whole callstack including the stack frame of each function (that contains the incoming params and local vars of functions). If an exception is handled nowhere (and ACCESS_VIOLATION is rarely handled because it isnt useful to handle it) then the exception unwinds the whole callstack. Result: The local vars and incoming parameters of the active function calls (at the time of exception) simply get destroyed (in case of ACCESS_VIOLATION this unwind/destroy doesnt happen if your code is compiled without SEH code generation). This is not a problem in case of local vars and params that don't need extra steps to destroy like in case of int variable or in case of complex classes that are on the stack by value (RAII: if you have SEH turned on) but this can be extremely dangerous for example if you store FILE pointers or handled or other raw C pointers on the stack because those simply get leaked, the exception returns and the rest of the function that would release these handles/pointers will never get executed. If your code is compiled without SEH support then even RAII is useless and you leak even more things.

Go back to the code and analyse and code-review it; if possible create some (unit)test functions to try to catch the offensive code.

Are you certain you are not leaking memory ? missing a few free/delete here and there ?

Are you certain you are checking all pointers (against NULL/nullPtr) before using them ?


There are tools to catch memory leaks, VLD is simple and free (http://vld.codeplex.com/[^] ) it should help you get a good start.


Good luck.
 
Share this answer
 
Comments
chait301 28-Sep-13 8:48am    
I did the analysis & found that in several interfaces the check for NULL pointer is not done. The access violation is is due to this reason. Windows internally handles this so does not causes application to crash. However can this be a cauase of memory leak.
secondly does it make sense to add check for NULL pointer for every interface? will it affect the performance?
First, caculate the size of each data structure in your codes.

Second, debug the program and then find out what size the one you have ignored is.

Then, go back and trace this kind objects in your codes.

I have meet this problem, and have found the cause like this.I hope it will help you!
 
Share this answer
 

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