PDA

Click to See Complete Forum and Search --> : Terminate Event Not Firing


Trev
Jan 30th, 2002, 03:59 PM
Hi,


I've written a small app which consists of a form in a standard exe and a class created in an ActiveX Dll. Unfortunately, I have to use circular references between objects, which has the nasty habit of keeping out of reference objects alive until the application ends.

I can live with this, except I need to get the class_terminate event when the application exits.... the plot thickens.

When running inside the IDE, the class terminate event is fired for each object when the form is closed and the application exits normally. This is expected. The problem happens when the application is compiled. The class_terminate event does not fire for the orphaned objects when compiled.

The “end” keyword is not used anywhere in the program and as far as I can see the program is exiting normally when the startup form is closed.

Oh, yeah, if the class is created as part of the standard exe, terminate events fire as normal, even when it’s compiled. I only seem to have the problem when the class if contained in an external dll.

I’m using VB6 Sp5 on Win 2000 Sp2, but first noticed it on VB6 SP2 on NT4 sp6.

I appreciate any help anyone can give me on this…

Cheers,

Trev.

Lethal
Jan 30th, 2002, 06:05 PM
Are you destroying all of your object variables before you shut down the application? When the component is compiled, the COM runtime is not performing the necessary clean up because you are aliasing objects (circular).


Set objX = Nothing

Trev
Jan 30th, 2002, 06:38 PM
Here's a quick sample application to demonstrate the effect.

Project1.vbp: Standard Exe that creates the object from class 1 (contained in Project 1)

Project2.vbp: ActiveX DLL that supplies Class 2 to Project1.vbp

Instructions:
1. Register "Project2.dll"
2. Open Project1.vbp
3. Run and click the "Run" Command Button
4. Notice the Msg Boxes for the Initialize events for both the class supplied by the exe and the class supplied by the DLL.
5. Click on the "Exit" command button. You Should Get Msgboxes for Both the DLL and Exe class terminates.
6. Run Project1.exe and repeat steps 3-5 above. Notice that the DLL terminate does not fire.



Interesting Things to Note:
The DLL does not have to be run in the IDE to demonstrate this effect, Only the hosting EXE
Classes supplied by out of process exes (ActiveX) fire their class terminate as soon as the variable goes out of scope, regardless of the circular reference inside the class. (let me know if you want a sample of this too).


In my application, I need to use circular references beween objects for various reasons (child objects catching events from a parent object etc.). Also, I cannot convert the DLL to an ActiveX exe because of performance reasons as well as the fact that I've never been able to get Data binding to work accross different processes (another story alltogether).

Hope this helps. I've tried every angle at this problem that I can think of (including compiling to PCode) and nothing works. Any ideas are very welcome!

Cheers for the help,

Trev.

Lethal
Jan 30th, 2002, 07:38 PM
Well, I did all the steps as you gave, and it works fine. Also, you should be using clean-up code in your class terminate events. For example, destroying your CircularReference object.

Trev
Jan 31st, 2002, 01:50 PM
Are you sure you got the terminate event from the DLL class when running the exe outside of the IDE? If so, what's your exact version of vb/os? I'm using VB6 Enterprise (SP5) on Windows 2000 (Sp2).

The exact behaviour on my machine is 2 "Init" message boxes when clicking run and 2 "Terminate" message boxes when clicking exit inside the IDE. When outside the IDE, I only get 1 terminate message box when clicking exit.

I know I should be cleaning up the circular reference in the class terminate handler, but if it doesn't fire when called from the exe, what's the point? I'm more interested in finding out why there is a such a big difference between the IDE and the compiled executable.

Cheers for your help.


Trev.

Yoram
Oct 7th, 2002, 08:42 AM
when running under IDE mode, your process and thread are does of the VB.EXE and all forms, controls and classes (includeing DLLs) are loaded into it's address space.
that means, in many temms, that you don't realy run a VB program of tour own but only interpretate it and run the VB.EXE code
that causes some problems:
DLLS are loaded on-demand by VB.EXE, unlike in your EXE when they are loaded in the begining of the application. same with unload DLLs, so VB.EXE performs an explicit unload of classes and DLLs when "terminateing" your applictaion.
when useing circular refernces uncarefully, UserControls mite stay loaded (in VB.EXE itself) after "Exit" in IDE mode.
hooking thread-messages (like QuitMessage) actualy hooks the VB.EXE main thread under IDE mode. so the hook callbacks will not be called when you "Exit". don't use QuitMessage-hook in palce of Terminate_event when you run it under IDE.

shortly, all behaveore of Load, Unload, Cleanups etc. is different when runing IDE mode.