By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,033 Members | 1,285 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,033 IT Pros & Developers. It's quick & easy.

CreateObject and Release

P: n/a
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.ReleaseComO bject(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.ReleaseComO bject(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.ReleaseComO bject(X)
....
End Sub
Y will say 0 even though I never Released X2 (only X)...

What is really going on here?
Thanks,
Shawn
Nov 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.ReleaseComO bject(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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 20 '05 #2

P: n/a
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

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Shawn,
My question is this: does this indicate that there is still a reference ordoes 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.ReleaseComO bject(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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

Nov 20 '05 #3

P: n/a
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
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Shawn,
My question is this: does this indicate that there is still a reference ordoes 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.ReleaseComO bject(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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

Nov 20 '05 #4

P: n/a
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." <le****@html.com> wrote in message
news:u7**************@TK2MSFTNGP09.phx.gbl...
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
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Shawn,
My question is this: does this indicate that there is still a reference ordoes 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.ReleaseComO bject(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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.


Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.