Home All Groups Group Topic Archive Search About
Author
19 Oct 2006 7:15 PM
nokia33948
Hi,
I have a small big problem with classes and objects..

In VB6 I create a new project, DLL ActiveX; I leave the default name
("progetto1" - in italian); then I rename the class module in "cls1"; I
save the class module with name "cls1.cls" and the project file with
name "clsT.vpb"; and then I add these few lines to the class module:

Private Sub Class_Initialize()
    Debug.Print "Class_Initialize()"
End Sub

Private Sub Class_Terminate()
    Debug.Print "Class_Terminate()"
End Sub

Public Sub ini()
    Debug.Print "INI"
End Sub

I set "5 - Multiuse" as Instancing for cls1, save again and run the
project.

VB.NET; I create a new project "windows application" type; I add a
button to the main form; I add as reference that "progetto1" saved
before (its simple since you can add a vpb project without compiling
it!); for the button click event I add the following:

        Dim k As Progetto1.cls1

        k = New Progetto1.cls1
        k.ini()
        k = Nothing

Save and run the VB.NET project too.

Now, pressing the button on the form I aspect to see in the "debug
window" of VB6: "Class_Initialize()", then "INI", then
"Class_Terminate()", but that does not happen! Only
"Class_Initialize()" and "INI" are printed, while "Class_Terminate()"
is only printed when I close the form!

Can someone explain me why this happens?

Sorry for my english...

I forgot to say that I use VB.NET 2003...

Grazie,
D.

Author
19 Oct 2006 7:23 PM
Theo Verweij
a) this is a .net newgroup, not a vb6 one
b) your problem is caused the "multiuse instancing"

nokia33948 wrote:
Show quoteHide quote
> Hi,
> I have a small big problem with classes and objects..
>
> In VB6 I create a new project, DLL ActiveX; I leave the default name
> ("progetto1" - in italian); then I rename the class module in "cls1"; I
> save the class module with name "cls1.cls" and the project file with
> name "clsT.vpb"; and then I add these few lines to the class module:
>
> Private Sub Class_Initialize()
>     Debug.Print "Class_Initialize()"
> End Sub
>
> Private Sub Class_Terminate()
>     Debug.Print "Class_Terminate()"
> End Sub
>
> Public Sub ini()
>     Debug.Print "INI"
> End Sub
>
> I set "5 - Multiuse" as Instancing for cls1, save again and run the
> project.
>
> VB.NET; I create a new project "windows application" type; I add a
> button to the main form; I add as reference that "progetto1" saved
> before (its simple since you can add a vpb project without compiling
> it!); for the button click event I add the following:
>
>         Dim k As Progetto1.cls1
>
>         k = New Progetto1.cls1
>         k.ini()
>         k = Nothing
>
> Save and run the VB.NET project too.
>
> Now, pressing the button on the form I aspect to see in the "debug
> window" of VB6: "Class_Initialize()", then "INI", then
> "Class_Terminate()", but that does not happen! Only
> "Class_Initialize()" and "INI" are printed, while "Class_Terminate()"
> is only printed when I close the form!
>
> Can someone explain me why this happens?
>
> Sorry for my english...
>
> I forgot to say that I use VB.NET 2003...
>
> Grazie,
> D.
>
Author
20 Oct 2006 6:06 AM
Tom Shelton
Try changing to the following:

>         Dim k As Progetto1.cls1
>
>         k = New Progetto1.cls1
>         k.ini()
           Marshal.ReleaseComObject (k)

HTH,

--
Tom Shelton
Author
20 Oct 2006 7:12 AM
nokia33948
Tom Shelton wrote:
> Try changing to the following:
>
> >         Dim k As Progetto1.cls1
> >
> >         k = New Progetto1.cls1
> >         k.ini()
>            Marshal.ReleaseComObject (k)
>
> HTH,
>
> --
> Tom Shelton

Thanks you very much Tom. That solved: both the problema and my
headache!

Rergards,
D.
Author
22 Oct 2006 3:44 AM
Jay B. Harlow
D.
Of course Marshal.ReleaseComObject may cause other problems & headaches!

Also you may want to consider Marshal.ReleaseComObject in a loop to
ensure that the COM objects are released (for .NET 2.0 I would recommend
Marshal.FinalReleaseComObject).

Taking into account all the potential problems that ReleaseComObject
introduces...

http://blogs.msdn.com/yvesdolc/archive/2004/04/17/115379.aspx
http://blogs.msdn.com/cbrumme/archive/2003/04/16/51355.aspx
http://samgentile.com/blog/archive/2003/04/17/5797.aspx

http://msdn2.microsoft.com/en-us/library/system.runtime.interopservic...

http://msdn2.microsoft.com/en-us/library/system.runtime.interopservic...

For details on when you should & when you shouldn't (call GC.Collect) see
the four web pages on the GC at:

http://www.tsbradley.net/Reading/CLR.aspx

Especially read these two:

http://blogs.msdn.com/ricom/archive/2004/11/29/271829.aspx
http://blogs.msdn.com/ricom/archive/2003/12/02/40780.aspx

Note to self:  I need to add the above links to my page on the CLR...

--
Hope this helps
Jay B. Harlow
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


Show quoteHide quote
"nokia33948" <nokia33***@yahoo.it> wrote in message
news:1161328334.552311.174330@h48g2000cwc.googlegroups.com...
>
> Tom Shelton wrote:
>> Try changing to the following:
>>
>> >         Dim k As Progetto1.cls1
>> >
>> >         k = New Progetto1.cls1
>> >         k.ini()
>>            Marshal.ReleaseComObject (k)
>>
>> HTH,
>>
>> --
>> Tom Shelton
>
> Thanks you very much Tom. That solved: both the problema and my
> headache!
>
> Rergards,
> D.
>