471,319 Members | 3,302 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 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 3633

"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Chris Capel | last post: by
4 posts views Thread by MC D | last post: by
13 posts views Thread by MuZZy | last post: by
4 posts views Thread by sunitabalakrishna | last post: by
5 posts views Thread by Chris | last post: by
reply views Thread by Gman | last post: by
4 posts views Thread by Rob | last post: by
8 posts views Thread by Varangian | last post: by
29 posts views Thread by Jerry Spence1 | last post: by

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.