473,408 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

CreateObject and Release

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
4 4514
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: wk6pack | last post by:
Hi, I'm getting this error on my asp page intermittently. One day it is fine, another day, it crashes a lot. I have searched the web and microsoft on this and they say it is a recordset...
6
by: MacKenzie | last post by:
The statement in an asp page: dim objShell Set objShell = WScript.CreateObject("WScript.Shell") now gives this error: VB err= 424,Object required If I use...
1
by: Raúl Martín | last post by:
I´ve a function in asp that run correctly but If I tried to change it forasp.net in asp: xmlHTTP = CreateObject("Microsoft.XMLHTTP") And I thought to use this sentence for asp.net but the...
12
by: karen | last post by:
Hi all : this is going to be a long post. So i apologize in advance :) i am converting a java program in VB right now. I am a java programmer by trade. so i am no expert in this department. I...
4
by: Jethro | last post by:
Hi all upgrade a VB6 Project, and am now stumped. The project contained 4 class modules. These were upgraded from : Public Class HTMLMail to :
2
by: C Williams | last post by:
Hi, I am having some problems with CreateObject and Powerpoint. I'm working from VB.NET with Powerpoint 2003. The code below is only ever called when powerpoint is already open. It's called...
7
by: dlasusa | last post by:
Sorry if this is the wrong group...I THINK I got to the right place...(oh...and I'm a newbie programmer...so please be gentle) Anyway I have a program that works fine when I run it from within...
1
by: Lynn Zou | last post by:
In our system , we use an ASP page to upload files but sometimes it doesnot work as it suppose to do and we found error log in Application Error as: Event Type: Error Event Source: Active...
3
by: Sagar | last post by:
I am working on a project where Server.CreateObject is replaced with CreateObject all over the project. Though I know that this will improve performance in terms of Memory overlhead because of how...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.