CreateObject and Release

  • Thread starter Thread starter Shawn B.
  • Start date Start date
S

Shawn B.

Greets,

If I say


Dim X As Object
X = CreateObject("Some.ProgID")
....
X = Nothing

I assume I'm missing the following

....
System.RunTime.InteropServices.Marshal.ReleaseComObject(X)
X = Nothing

Is supposed to be the correct way of doing this?



If I do it both ways, if I try to move the DLL in question it can't because
it is in use, even after I called ReleaseComObject(x) but when I exit the
application I can.

If I say

....
System.Runtime.InteropServices.Marshal.ReleaseComObject(X)
X = Nothing

GC.Collected()
GC.WaitForPendingFinalizers()


The same. Until I exit the application I can't move the DLL.


My question is this: does this indicate that there is still a reference or
does this indicate that it really hasn't been garbage collected?


If I say

Dim X As Object
Dim X2 As Object
Dim Y As Integer

X = CreateObject("X.Y")
Y = CreateObject("X.Y")
....
Y = System.Runtime.InteropServices.Marshal.ReleaseComObject(X)
....
End Sub


Y will say 0 even though I never Released X2 (only X)...

What is really going on here?


Thanks,
Shawn
 
Shawn,
My question is this: does this indicate that there is still a reference or
does this indicate that it really hasn't been garbage collected?

Neither. The DLL will not necessarily be unloaded even if all objects
created by it are released.

If I say

Dim X As Object
Dim X2 As Object
Dim Y As Integer

X = CreateObject("X.Y")
Y = CreateObject("X.Y")
...
Y = System.Runtime.InteropServices.Marshal.ReleaseComObject(X)

Did you mean

X2 = CreateObject("X.Y")

Y will say 0 even though I never Released X2 (only X)...

Did you expect something else?



Mattias
 
Perhaps I was misunderstanding the return result then? I thought it
returned the number of references to that object.. so if I created two
instances of it, and release one, then I thought it would return there was
still one reference... of course, I may have misunderstood?


Thanks,
Shawn
 
Yes, I did expect something else. After further investigation, the MSDN
Docs say the ReleaseComReference decrements the reference count, not resets
it. What I was expecting and still expect and ?should? expect is that if I
have two objects each containing a seperate instance and I release 1 of
those intstances, there would still be 1 reference remaining. I woudl exect
ReleaseComReference to return a '1' and not a zero.


Thanks,
Shawn
 
Each object has its own reference count. Object X has had its one reference
released and so now has a reference count of 0 whereas object X2 has not had
its reference released so it has a reference count of 1. The objects use the
reference counts to control their lifetimes.

If you assigned X2 to another variable, let's say X3, then the object
instance pointed to by X2 and X3 would have a reference count of 2 and a
call to ReleaseComObject on either X2 or X3 would return 1.

Shawn B. said:
Yes, I did expect something else. After further investigation, the MSDN
Docs say the ReleaseComReference decrements the reference count, not resets
it. What I was expecting and still expect and ?should? expect is that if I
have two objects each containing a seperate instance and I release 1 of
those intstances, there would still be 1 reference remaining. I woudl exect
ReleaseComReference to return a '1' and not a zero.


Thanks,
Shawn
 
Back
Top