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

Dispose of COM

Hi,

I have a web service that calls a COM object written in Uniface and
unfortunatley we don't have the opportunity to rewrite this component. This
COM object is not thread safe, in fact when two threads access the object it
will fail spectactularly! This is fine, I have created a class that only
allows one thread to call into the COM object at a time. The esyv0005Class
is the .NET Interop assembly created from our COM object.

internal class RequestBroker {
private static object mLock = new object();

........... more methods

private string InternalExecute(string parameters, string serviceName) {
lock (mLock) {
string resultXml;
ePathway.Request.Broker.esyv0005Class service = new
ePathway.Request.Broker.esyv0005Class();
service.exec(serviceName, parameters, out resultXml);

// The important lines here
// service = null;
// GC.Collect();

return resultXml;
}
}
}

If i leave the two lines commented out we rely on the GC to at its leisure
dispose of the interop object which intern will free the reference to the
underlying COM object. The problem with this is that our application fails
when stress tested because the object isn't being disposed of immediately
and we receive a dreaded Uniface -57 error. If I uncomment the calls to
GC.Collect() i then have a hell of alot of calls for the GC to do its thing,
especially underload.

Now the .Net interop class (esyv005Class) doesn't implement IDispose so
thats not a solution here, is there a way of having the interop object GC
immediately without the overhead of calling GC.Collect() ? I have tried
making the variable a member of the class and only instantiating the object
once, thereby eliminating the need to dispose of the object, however I
encountered periodic errors. Any other alternatives that you can see ?

tia
andrew
Nov 15 '05 #1
3 3183
andrew lowe wrote:
is there a way of having the interop
object GC immediately without the overhead of calling GC.Collect() ?


No but even so, simply setting your reference to null is not enough to
ensure it does get collected. Be sure to call
System.Runtime.IntropServices.Marshal.ReleaseComOb ject first in order to
decrement the reference count on the ComObject itself.

You might also consider implementing IDispose on the object's container.
In the Dispose method, check to if the object is set to null. If not,
release it then set it to null.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
Nov 15 '05 #2

"Frank Oquendo" <fr*******@acadx.com> wrote in message
news:OU**************@TK2MSFTNGP09.phx.gbl...
No but even so, simply setting your reference to null is not enough to
ensure it does get collected. Be sure to call
System.Runtime.IntropServices.Marshal.ReleaseComOb ject first in order to
decrement the reference count on the ComObject itself.


Thanks Frank, that solves the problem without having to call GC.Collect(),
you beauty

andrew

Nov 15 '05 #3
Frank's reply is a good one... You actually have a component that represents
a finite (in general terms) resource, "Uniface"

Normally one should implement IDisposable on these classes to "inform" users
of the class that the Dispose method should be called before releasing the
reference.

Your code looks good to me, to prevent that re-entrancy problem, and Frank's
suggestion will help manage the "finite resource".

--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
er**@cc.ensoft-software.com [remove the first "CC."]

"Frank Oquendo" <fr*******@acadx.com> wrote in message
news:OU**************@TK2MSFTNGP09.phx.gbl...
andrew lowe wrote:
is there a way of having the interop
object GC immediately without the overhead of calling GC.Collect() ?


No but even so, simply setting your reference to null is not enough to
ensure it does get collected. Be sure to call
System.Runtime.IntropServices.Marshal.ReleaseComOb ject first in order to
decrement the reference count on the ComObject itself.

You might also consider implementing IDispose on the object's container.
In the Dispose method, check to if the object is set to null. If not,
release it then set it to null.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)

Nov 15 '05 #4

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

Similar topics

3
by: faktujaa | last post by:
Hi All, A small confusion. I have defined a connection class that has System.Data.IDbConnection as a member variable and implements IDisposable interface. I have implemented Dispose method to call...
4
by: RiteshDotNet | last post by:
..net Frame work 1. Dispose Method what it does ? A. who its call / when it calls ? B. Is it fire automatically ? c. When dispose method is call what it does ? D. Release a Object from memory or...
11
by: Ken Durden | last post by:
I am in search of a comprehensive methodology of using these two object cleanup approaches to get rid of a number of bugs, unpleasantries, and cleanup-ordering issues we currently have in our...
10
by: Henrik Dahl | last post by:
Hello! After I've finished using an instance of the SqlCommand class, should I then invoke Dispose() on the instance. I suppose so, as there is a Dispose method, but what does it actually...
16
by: Daniel Mori | last post by:
If an object implements the IDisposable interface (regardless if its a framework object or a user object), should I always dispose of that object out of principle?
3
by: Maxim | last post by:
Hi! According to documenation, if we need to release some umanaged resources manually, we need to implement IDisposable interface with single Dispose method. I am just wondering, what will...
1
by: Billy | last post by:
Hello... I'm trying to make a database access class for an asp.net application. When I run my application, the Garbage Collecter doesn't seems to unload the memory attributed to my...
156
by: Dennis | last post by:
Ok, I'm trying to dispose of every object that I create that has a dispose method based on advice from this newsgroup. However, I'm not sure how to dispose of the following object that was created...
71
by: active | last post by:
In the main program I check to see if a certain form has been disposed. Does it make sense in that form's FormClosed event to do: Me.Dispose to make sure it is disposed the next time I check. Or...
44
by: Smokey Grindle | last post by:
I have a list box on my form, but I need to databind it to a data table that is a private member of the form's class... so I basically have Public Class MyForm priate m_MyTable as new datatable...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.