From: BeeJ on
I have some ActiveX code, both EXE and DLL that I wrote.
It all seems to work but I occasionally have a weird thing happen.
The code runs through Class_Terminate before Class_Initialize.
I do not see why but it may be because I am working in the IDE and
maybe this app is not closing as I thought or does the Set cEntry = Me
make a double instantiation?
There is no Main module and this does not start, Class_Initialize,
until the app is instantiated in the main program.
This is one of the ActiveX that do not want to terminate. All the
others do what they are supposed to.

The entry class named 'Entry' has this:
' =================================================
Public Event DevStatus(sStatus As String)

Private Sub Class_Initialize()
Set cEntry = Me ' to allow access from
' modules back to this class
Startup ' code in module
End Sub 'Class_Initialize

Private Sub Class_Terminate()
On Error GoTo Class_TerminateErr
ShutDown ' code in module
Set cEntry = Nothing
Class_TerminateExit:
Exit Sub
Class_TerminateErr:
Debug.Assert False
Resume Class_TerminateExit
End Sub 'Class_Terminate

Friend Sub StatusMsg(sMsg As String)
RaiseEvent DevStatus(sMsg)
End Sub 'StatusMsg

' =================================================

The module mdlEntry has this:
' =================================================
Public cEntry As Entry

Private Sub Msg(sMsg as String)
cEntry.StatusMsg sMsg
End Sub
' =================================================


From: ralph on
On Sun, 08 Aug 2010 16:58:38 -0700, BeeJ <nospam(a)nowhere.com> wrote:

>I have some ActiveX code, both EXE and DLL that I wrote.
>It all seems to work but I occasionally have a weird thing happen.
>The code runs through Class_Terminate before Class_Initialize.
>I do not see why but it may be because I am working in the IDE and
>maybe this app is not closing as I thought or does the Set cEntry = Me
>make a double instantiation?

Not a double instantiation - but does call Terminate.

The Initialize event is fired only once when the object is created.
Re-assigning to new references doesn't fire the event.

However, the Terminate event is fired when the object is set to
Nothing, when the last referenced object is 're-set' to a new
reference using the Set/New, or when it is re-assigned to a new
instance.

-ralph
From: Tom Shelton on
ralph pretended :
> On Sun, 08 Aug 2010 16:58:38 -0700, BeeJ <nospam(a)nowhere.com> wrote:
>
>> I have some ActiveX code, both EXE and DLL that I wrote.
>> It all seems to work but I occasionally have a weird thing happen.
>> The code runs through Class_Terminate before Class_Initialize.
>> I do not see why but it may be because I am working in the IDE and
>> maybe this app is not closing as I thought or does the Set cEntry = Me
>> make a double instantiation?
>
>

<snip>

>
> However, the Terminate event is fired when the object is set to
> Nothing, when the last referenced object is 're-set' to a new
> reference using the Set/New, or when it is re-assigned to a new
> instance.

I think your trying to say that the terminate event fires when the last
reference is released - correct?

Just to be clear - doing a Set cEntry = Me would cause the reference
count of the object pointed to by me to be incremented by one. And
then when cEntry went out of scope, the reference count would be
reduced by one - but, the terminate event would not fire... Since the
Me reference is still arround.

One cause of the described behavior maybe a circular reference....
That can definately cause a memory leak and an exe to hang around in
memory rather then shutdown properly. Hard to know without seeing some
code though....

--
Tom Shelton


From: BeeJ on
Tom Shelton expressed precisely :
> ralph pretended :
>> On Sun, 08 Aug 2010 16:58:38 -0700, BeeJ <nospam(a)nowhere.com> wrote:
>>
>>> I have some ActiveX code, both EXE and DLL that I wrote.
>>> It all seems to work but I occasionally have a weird thing happen.
>>> The code runs through Class_Terminate before Class_Initialize.
>>> I do not see why but it may be because I am working in the IDE and maybe
>>> this app is not closing as I thought or does the Set cEntry = Me make a
>>> double instantiation?
>>
>>
>
> <snip>
>
>>
>> However, the Terminate event is fired when the object is set to
>> Nothing, when the last referenced object is 're-set' to a new
>> reference using the Set/New, or when it is re-assigned to a new
>> instance.
>
> I think your trying to say that the terminate event fires when the last
> reference is released - correct?
>
> Just to be clear - doing a Set cEntry = Me would cause the reference count of
> the object pointed to by me to be incremented by one. And then when cEntry
> went out of scope, the reference count would be reduced by one - but, the
> terminate event would not fire... Since the Me reference is still arround.
>
> One cause of the described behavior maybe a circular reference.... That can
> definately cause a memory leak and an exe to hang around in memory rather
> then shutdown properly. Hard to know without seeing some code though....

On exiting, in Class_Terminate I do

Set cEntry = Nothing

So that should decrement.


From: Dee Earley on
On 09/08/2010 17:17, BeeJ wrote:
> On exiting, in Class_Terminate I do
>
> Set cEntry = Nothing
>
> So that should decrement.

It will never decrement as cEntry still has a reference to it so the
Terminate event will never be called.

You must have an explicit "Close" method that unsets cEntry and allows
it to terminate.

--
Dee Earley (dee.earley(a)icode.co.uk)
i-Catcher Development Team

iCode Systems

(Replies direct to my email address will be ignored.
Please reply to the group.)