Solved

Handling abnormal program termination

Posted on 2001-09-03
8
470 Views
Last Modified: 2008-02-01
Dear Friends,

   This is my problem, I have written a COM DLL which creates a very complex link list. I have handled all errors inside the COM DLL and i also clear the Heap asap.

   The average number of nodes in the link list will be about 10,000 to 15,000 nodes.

   The program which uses this COM dll may not handle the exceptions (Client can be in Visual Basic, C++ or Visual C++), Now if the program that uses this COM dll terminates abnormally then how can i release all the memory allocated by the COM dll in the heap?

   Is there any way i could handle this, because since it creates about 10,000 to 15,000 nodes, the memory usage is bit high and i want to free the heap even in case of abnormal termination.

Thank you in advance
Arvind T.N
0
Comment
Question by:Arvindtn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 49

Expert Comment

by:DanRollins
ID: 6450149
The OS should clean up after the client process terminates.  Have you checked the memory usage status in this event?

-- Dan
0
 

Author Comment

by:Arvindtn
ID: 6450173
Yes, the memory in the heap is not cleared, I think the OS will clear only the stack of the client process. Some times to clean up the memory the client needs to have a restart.

Is there any way in which the DLL could trap the abnormal termination?
0
 
LVL 4

Expert Comment

by:AssafLavie
ID: 6450540
You have to differenciate beteen memory leaks and thrown exception.
The latter is easy. Use a try catch inside every STDMETHOD implementation and translate all errors into HRESULTs.

There's no cure for memory leaks aside from:
1. Writing good code.
2. Using process isolation (i.e. an EXE COM server)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 6450590
If it is an inproc server DLL, the OS will do the cleanup along with the client cleanup when the client terminates. However, if you want to have control about this, you could install your own 'UnhandledExceptionFilter()' that does this for you. It can be installed by calling 'SetUnhandledExceptionFilter()', which replaces the existing top-level exception filter (default behaviour: displays an Application Error message box and causes the exception handler to be executed) with your own one to do the cleanup before going on in the handler chain:

LONG CleanupExceptionFilter(
  STRUCT _EXCEPTION_POINTERS *ExceptionInfo   // address of
                                              // exception info
) {

 DoCleanup();

 return (EXCEPTION_CONTINUE_SEARCH);
}
 


// in your code, preferrably when your COM server initializes

// ...

SetUnhandledExceptionFilter ( CleanupExceptionFilter);

0
 

Author Comment

by:Arvindtn
ID: 6450688
I will explain my problem more clearly.

  iam using inproc server dll in a Visual Basic Standard EXE. The VB Server connects to a Socket and starts to receive messages, These messages are used to create a complex Link List. The size of the Link List is about 10,000 to 15,000 nodes, thus using abt 1 to 3MB of memory.
Now if the VB program terminates abnormally how will i clear this 1 To 3MB of memory allocated to the inproc Dll through the 2new operator", under normal cirumstances, the memory can be cleared either by destroying the object (using delete operator) or by calling a COM Method.

  If the VB program terminates abnormally, will the OS clear the heap of the Inproc DLL?

  If not then how can i do this?

    Regarding the Unhandled exceptions, where should i write it, in the Inproc DLL or in the Server?

   I can solve the problem by handling the exceptions in the Program which utilizes the Inproc DLL, but if the Inproc DLL is used by some other program, then there is a possibility of abnormal termination.

   The abnormal termination can be due to unstructured error handling in the Program that uses my Inproc DLL, In this case how can i free the memory in the heap, though the error was not raised from the Inproc Dll but from the program that uses the Inproc DLL?




   
0
 
LVL 86

Expert Comment

by:jkr
ID: 6450698
>>If the VB program terminates abnormally, will the OS
>>clear the heap of the Inproc DLL?

Yes, it will, even though it's good practise to do that by yourself.

>>Regarding the Unhandled exceptions, where should i write
>>it, in the Inproc DLL or in the Server?

In the inproc DLL - BTW, regarding terminology: The DLL is an inproc COM server, so it's actually the same...
0
 

Author Comment

by:Arvindtn
ID: 6450736
Iam now implementing the 'SetUnhandledExceptionFilter' in my Inproc DLL, will get back soon

0
 
LVL 10

Expert Comment

by:makerp
ID: 6450896
what about putting the clean up code in the DLLMain function, put it in the case statement that gets called when a process is finished with the dll, perhaps this is called regardless of the type of program termination.

i dont know if that will work, its just a thought

Paul
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question