Disposing Com+ Object

S

sternr

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
 
W

Willy Denoyette [MVP]

| 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.
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
S

sternr

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
 
N

Nicholas Paldino [.NET/C# MVP]

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).
 
S

sternr

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
 
W

Willy Denoyette [MVP]

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.



| 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
|
 
S

sternr

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
 
W

Willy Denoyette [MVP]

| 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.
 
S

sternr

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
 
W

Willy Denoyette [MVP]

| 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top