473,473 Members | 2,031 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 3884

"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
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.