473,320 Members | 1,930 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.

Disposing Com+ Object

Hey,
I have a C# assembly that inherits from ServicedComponent and is
installed in the Com+ with ServerActivation.
And I have a C++ application that calls this Com+ object successfully,
But the problem is that I cant seem to dispose the object!

I tried calling the following code:
myComPlus->Release();
myComPlus = NULL;
delete myComPlus;
CoUninitialize();

The reason I posted it here (DotNet group) is that the same code works
when the Com+ object is NOT a DotNet Assembly.

Apparently I need to find a way to the ServicedComponent.DisposeObject
from the C++ code!
Is there a way to dispose a .Net assembly from the Com+ by C++ Code?
Thanks ahead

sternr

Jan 9 '06 #1
10 3863

"sternr" <St****@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
| Hey,
| I have a C# assembly that inherits from ServicedComponent and is
| installed in the Com+ with ServerActivation.
| And I have a C++ application that calls this Com+ object successfully,
| But the problem is that I cant seem to dispose the object!
|
| I tried calling the following code:
| myComPlus->Release();
| myComPlus = NULL;
| delete myComPlus;
| CoUninitialize();
|
| The reason I posted it here (DotNet group) is that the same code works
| when the Com+ object is NOT a DotNet Assembly.
|
| Apparently I need to find a way to the ServicedComponent.DisposeObject
| from the C++ code!
| Is there a way to dispose a .Net assembly from the Com+ by C++ Code?
| Thanks ahead
|
| sternr
|

You have to call Dispose() explicitely.

Willy.
Jan 9 '06 #2
You need to show more information. First, can you show some of the
code?

In the C++ client, you need to only call Release, and it should release
the object.

On the .NET side, if you are using JIT activation (which is
automatically set if you are using transactions), then you have to make sure
you apply the AutoComplete attribute to your methods on the object which are
exposed. Either that, or you have to make sure that you set the
DeactivateOnReturn property on the ContextUtils class to true, to indicate
that the object should be deactivated.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"sternr" <St****@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Hey,
I have a C# assembly that inherits from ServicedComponent and is
installed in the Com+ with ServerActivation.
And I have a C++ application that calls this Com+ object successfully,
But the problem is that I cant seem to dispose the object!

I tried calling the following code:
myComPlus->Release();
myComPlus = NULL;
delete myComPlus;
CoUninitialize();

The reason I posted it here (DotNet group) is that the same code works
when the Com+ object is NOT a DotNet Assembly.

Apparently I need to find a way to the ServicedComponent.DisposeObject
from the C++ code!
Is there a way to dispose a .Net assembly from the Com+ by C++ Code?
Thanks ahead

sternr

Jan 9 '06 #3
Willy - There is no Dispose method, do you mean I need to implement
IDisposeable?
Nicholas - In the managed code I dont use any of the Com+ features - no
Object Pooling,JITA or Transactions...
And as for the C++, here's the full code:

MULTI_QI mqi;
HRESULT hr;
CLSID cls;
objComPlus::IMyCls* objCom;
COSERVERINFO cs;
ZeroMemory(&mqi,sizeof(mqi);
cs.pAuthInfo=NULL;
mqi.pIID = &__uuidof(objComPlus::_MyCls);
mqi.pitf=objCom;
mqi.hr=0;
hr=CoInitialize(NULL);
if (SUCCEEDED(hr))
{
hr=CLSIDFromProgID(OLESTR("objComPlus.MyCls"),&cls );
if (SUCCEEDED(hr))
{
mqi->QueryInterface(__uuidof(objComPlus::_MyCls),(void **) &objCom);
/*
..
Call some methods from objCom
..
*/
ht=CoDisconnectObject(objCom,0); // This line isnt needed but it doesnt
hurt trying
objCom->Release();
objCom=NULL;
delete objCom;
CoUninitialize();
}
}

Hope this helps!
Thanks again for your help!

sternr

Jan 9 '06 #4
sternr,

First, if you are not using any of the COM+ features, then why are you
using COM+? You shouldn't use it at all, since you can expose your object
to COM without it being hosted in COM+. Is it because you want it hosted on
another machine?

As for your code accessing the object, it doesn't make much sense.
First, you care not showing the call to create the object, and why you need
a MULTI_UI structure for multiple interfaces (especially since you are using
just one, it would seem).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"sternr" <St****@gmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
Willy - There is no Dispose method, do you mean I need to implement
IDisposeable?
Nicholas - In the managed code I dont use any of the Com+ features - no
Object Pooling,JITA or Transactions...
And as for the C++, here's the full code:

MULTI_QI mqi;
HRESULT hr;
CLSID cls;
objComPlus::IMyCls* objCom;
COSERVERINFO cs;
ZeroMemory(&mqi,sizeof(mqi);
cs.pAuthInfo=NULL;
mqi.pIID = &__uuidof(objComPlus::_MyCls);
mqi.pitf=objCom;
mqi.hr=0;
hr=CoInitialize(NULL);
if (SUCCEEDED(hr))
{
hr=CLSIDFromProgID(OLESTR("objComPlus.MyCls"),&cls );
if (SUCCEEDED(hr))
{
mqi->QueryInterface(__uuidof(objComPlus::_MyCls),(void **) &objCom);
/*
.
Call some methods from objCom
.
*/
ht=CoDisconnectObject(objCom,0); // This line isnt needed but it doesnt
hurt trying
objCom->Release();
objCom=NULL;
delete objCom;
CoUninitialize();
}
}

Hope this helps!
Thanks again for your help!

sternr

Jan 9 '06 #5
Hey Nicholas,

I want to use Com+'s features but for testing purposes I'm not using
any of them...
And about the MULTI_UI - my object has multiple interfaces, I didn't
show use of them because it's not related to my problem...

I'm pretty sure the problem lies in disposing managed Com+ from C++
code as a hole and not in my particular piece code - since the same
code works for unmanaged Com+ objects...

sternr

Jan 9 '06 #6
If your COM+ class is holding unmanaged resources, you should implement
IDisposable if you want deterministic clean-up of these resources, after all
it's a managed class so .NET design guidelines apply here. Else your object
life time and the resource clean-up is dictated by the GC.
Note that calling Release on the object interface from unmanaged code should
make the object available for GC collection, but again this is something
that happens at some point in time (non-deterministically).

Willy.

"sternr" <St****@gmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
| Willy - There is no Dispose method, do you mean I need to implement
| IDisposeable?
| Nicholas - In the managed code I dont use any of the Com+ features - no
| Object Pooling,JITA or Transactions...
| And as for the C++, here's the full code:
|
| MULTI_QI mqi;
| HRESULT hr;
| CLSID cls;
| objComPlus::IMyCls* objCom;
| COSERVERINFO cs;
| ZeroMemory(&mqi,sizeof(mqi);
| cs.pAuthInfo=NULL;
| mqi.pIID = &__uuidof(objComPlus::_MyCls);
| mqi.pitf=objCom;
| mqi.hr=0;
| hr=CoInitialize(NULL);
| if (SUCCEEDED(hr))
| {
| hr=CLSIDFromProgID(OLESTR("objComPlus.MyCls"),&cls );
| if (SUCCEEDED(hr))
| {
| mqi->QueryInterface(__uuidof(objComPlus::_MyCls),(void **) &objCom);
| /*
| .
| Call some methods from objCom
| .
| */
| ht=CoDisconnectObject(objCom,0); // This line isnt needed but it doesnt
| hurt trying
| objCom->Release();
| objCom=NULL;
| delete objCom;
| CoUninitialize();
| }
| }
|
| Hope this helps!
| Thanks again for your help!
|
| sternr
|
Jan 9 '06 #7
But if my client was a .Net application, I could'v called
ServicedComponent.DisposeObject and force
The Com+ object to be disposed!
Isn't there a way to copy the behaviour of the DisposeObject method to
my C++ application, or maybe call COM wrapper for EnterpriseServices so
that I could call directly the DisposeObject?
Thanks again

sternr

Jan 9 '06 #8

"sternr" <St****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
| But if my client was a .Net application, I could'v called
| ServicedComponent.DisposeObject and force
| The Com+ object to be disposed!
| Isn't there a way to copy the behaviour of the DisposeObject method to
| my C++ application, or maybe call COM wrapper for EnterpriseServices so
| that I could call directly the DisposeObject?
| Thanks again
|
| sternr
|

No there isn't, don't forget that you are at the unmanaged side, so you are
in reality talking to a CCW, while calling into a managed COM+ application
from managed talks through a RCW (and DCOM) to access the COM+ server hosted
class.

Note also this:
From the docs....

It is preferable to use the Dispose design pattern rather than
DisposeObject.

Realy, keep your managed classes consistent with the Dispose pattern, it
doesn't matter whether they are called from managed or unmanaged, you have
to explicitely call Dispose().

Willy.


Jan 9 '06 #9
Hey Willy,
In continue to what you said, I exposed the Dispose void in the Managed
Com+ object,
and inside this void I call - base.Dispose(true).
But when I try to call this new method from my C++ application, I get
an access violation...
Any idea?
Thanks,

sternr

Jan 10 '06 #10

"sternr" <St****@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
| Hey Willy,
| In continue to what you said, I exposed the Dispose void in the Managed
| Com+ object,
| and inside this void I call - base.Dispose(true).
| But when I try to call this new method from my C++ application, I get
| an access violation...
| Any idea?
| Thanks,
|
| sternr
|

Not sure what you mean exactly with "access violation", but I can't believe
this is thrown when calling Dispose().
Please post the exact exception message and the call stack.
Also note that you don't have to implement IDisposable unless your class
owns unmanaged resources (or owns types which themselves owns resources) you
want to release deterministically. If this is not the case, you should not
implement this interface.
To me it looks like you are confusing object life time with resource
acquisition/release, don't expect your object will go away when you call
Dispose, it will only do so if it's no longer referenced when the GC comes
along.
So the question you should ask yourself is - do I own unmanaged resources
and, do I want them to release deterministically. Don't implement
IDisposable If one of the answers is no.

Willy.
Jan 10 '06 #11

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

Similar topics

15
by: Chris Capel | last post by:
I've heard a lot of talk on the forum about memory managment in .NET and disposing things, finalizing them, garbage collection, etc. My question is which managed types need to be disposed after...
4
by: MC D | last post by:
Question: If I have a class which has a property which is a collection of widget objects (an arrayList of widgets), and both the containter class and the widget class implement the IDisposable...
13
by: MuZZy | last post by:
Hi, Just wanted to make sure i get it right: consider this class: // =========== START CODE ============= class Test { private SqlConnection con = null; public void Connect() { con = new...
4
by: sunitabalakrishna | last post by:
Hi, Could someone explain me if I need to implement the Dispose pattern on managed objects that hold objects which have longer life or is a singleton? Example - Class A holds Class Global...
5
by: Chris | last post by:
I have a form that requires drawing custom lines on it. The color of the lines is suppose to be the same as the forcolor of the form. Am I doing this the most efficent and correct way? ...
0
by: Gman | last post by:
Hi, Objective: Draw a grid on a bitmap and set this as a panel's image. (Rather than draw the grid directly on the panel and redraw repeatedly in the paint event.) Problem: It works fine....
4
by: Rob | last post by:
Hi all, I wrote myself a little function to do my own housekeeping, I can call it by using Dispose(object) Within this function there's a select case statement which will then, based on...
8
by: Varangian | last post by:
Hello, was wondering of how to dispose of managed resources? or referencing every member of a class to null will release resources...? http://www.marcclifton.com/tabid/79/Default.aspx...
29
by: Jerry Spence1 | last post by:
I'm rather confused as to whether something should be disposed of, or not. What is the general rule? How can you be sure of doing the right thing? I've heard about disposing unmanaged resources but...
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: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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.