473,394 Members | 2,002 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,394 software developers and data experts.

.NET: garbage collection for data type returrned from COM Interop

JC
How does garbage collection work in C# and VB.NET for data returned from COM
object? For example, a COM object written in C++ returns a SAFEARRAY to C#
or VB.NET, will this SAFEARRAY (mapped to type[] in C# or VB.NET) be garbage
collected? Similarily, if C# or VB.NET pass SAFEARRAY to the COM object via
COM method invocation, will this SAFEARRAY be garbage collected after the
call?

My guess to answers for both questions is yes. Thanks.
Nov 19 '05 #1
1 3262
JC,

When you pass a SAFEARRAY from unmanaged code to managed code, what
happens as far as garbage collection depends on the types that are in the
safearray.

First off, the safearray is always released.

Before it is released, the runtime is going to marshal the elements from
the safearray from the unmanaged realm over to the managed realm. If the
safearray contains primitive types, then those are marshaled across into the
new managed array. If the safearray contains interface pointers, then those
are marshaled across, and the reference count is incremented by one on each
of them, assuring that they are alive when accessed by managed code. Then,
when the SAFEARRAY is deallocated (when the marshaling is complete), the
reference count will be reduced by one (which is fine, since the initial
entry of the item into the safe array should have incremented the reference
count by one), resulting in a net change of plus one for the reference
count, that reference being held by the managed wrapper for the COM object.

Now, these managed representations are subject to the same rules as any
other managed object, and will be garbage collected if they can not be
reached. When GCed, the reference count will be decremented by one, unless
a call to ReleaseComObject is made on the Marshal class, passing the COM
object. This will reduce the reference count by one at that point, which is
good practice, releasing it when you need it.

As for going the other way, when marshaling a .NET object into the
unmanaged realm, the runtime keeps a reference to it. When the COM-callable
wrapper is released (the reference count is set to zero), then the runtime
releases the reference, and it is subject to garbage collection, assuming
there are no other references.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"JC" <ch***@platform.nospam.com> wrote in message
news:u8**************@TK2MSFTNGP10.phx.gbl...
How does garbage collection work in C# and VB.NET for data returned from COM object? For example, a COM object written in C++ returns a SAFEARRAY to C# or VB.NET, will this SAFEARRAY (mapped to type[] in C# or VB.NET) be garbage collected? Similarily, if C# or VB.NET pass SAFEARRAY to the COM object via COM method invocation, will this SAFEARRAY be garbage collected after the
call?

My guess to answers for both questions is yes. Thanks.

Nov 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: JC | last post by:
How does garbage collection work in C# and VB.NET for data returned from COM object? For example, a COM object written in C++ returns a SAFEARRAY to C# or VB.NET, will this SAFEARRAY (mapped to...
12
by: Pankaj | last post by:
Hi all, I have write a GUI application(in 4-6 weeks), which is quite rich in functionality and Uses COM to connect to other parts of my software. I have to support 98,2000 and XP and enable...
3
by: Sai Kit Tong | last post by:
Hi, I am developing a new application running on Windows platform that needs to interface with existing legacy code - written in basic C / C++. I am trying to evaluate Java vs C#...
3
by: Sai Kit Tong | last post by:
I posted for help on legacy code interface 2 days ago. Probably I didn't make it clear in my original mail. I got a couple of answers but none of them address my issues directly (See attached...
16
by: Ben Hannon | last post by:
Hi, I'm writting a COM Class in VB.NET to be used in a VB6 project (Tired of the VB6 hassles with cloning and serializing an object). All my classes I need cloneable/serializable are now in a...
6
by: Scott M. Lyon | last post by:
As I mentioned in my other post, I'm attempting to, using COM Interop so I can update existing VB6 code to (for several specific functions) return a Hashtable from a .NET library. I've had...
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
0
by: shamirza | last post by:
· When was .NET announced? Bill Gates delivered a keynote at Forum 2000, held June 22, 2000, outlining the .NET 'vision'. The July 2000 PDC had a number of sessions on .NET technology, and...
56
by: Johnny E. Jensen | last post by:
Hellow I'am not sure what to think about the Garbage Collector. I have a Class OutlookObject, It have two private variables. Private Microsoft.Office.Interop.Outlook.Application _Application =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.