Classes and Objects

N

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.
 
T

Theo Verweij

a) this is a .net newgroup, not a vb6 one
b) your problem is caused the "multiuse instancing"
 
N

nokia33948

Tom said:
Try changing to the following:

Marshal.ReleaseComObject (k)

HTH,

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

Rergards,
D.
 
J

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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top