473,484 Members | 1,667 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

COM Events passing Object as parameter (problem)

I have a ATL COM object that is loaded through Interop in a C# application.
The COM object fires an event and one of the parameters is another object
created inside the object. This object has a property on it that is also
another object.
private void pIObj_EventProc(MObj.MainObj pObj)
{
MObj.Obj mainobj = pObj.Obj;
// IF I DON'T DO THE NEXT LINE, THE APPLICATION HANGS AT SOME LATER TIME
// THE UI DOESN'T EVEN UPDATE.
System.Runtime.InteropServices.Marshal.ReleaseComO bject(mainobj);
}

Property in ATL Object that returns another object...
HRESULT CMainObj::get_Obj(IObj** pVal)
{
IObj* pIObj= NULL;
CComObject<CObj>* pObj;
if (S_OK == CComObject<CObj>::CreateInstance(&pObj)
&& S_OK == pObj->QueryInterface(IID_IObj, (void**)&pIObj))
{
*pVal = pIObj;
return S_OK;
}
}

I don't fully understand interop but I'm thinking this could be the source
of my problem. Is there a way to make this reliable from the COM object
perspective? I'd rather not require the user to do the ReleaseComObject in
order to prevent their application from hanging at some random time.
Everything works correctly if the host is C++ or JScript or other such
languages.

Any help would be appreciated.
Dec 9 '05 #1
4 3874
"Jayme Pechan" <ja**********@whitefeld.com> wrote in message
news:O1*************@TK2MSFTNGP15.phx.gbl...
I have a ATL COM object that is loaded through Interop in a C# application.
The COM object fires an event and one of the parameters is another object
created inside the object. This object has a property on it that is also
another object.
private void pIObj_EventProc(MObj.MainObj pObj)
{
MObj.Obj mainobj = pObj.Obj;
// IF I DON'T DO THE NEXT LINE, THE APPLICATION HANGS AT SOME LATER
TIME
// THE UI DOESN'T EVEN UPDATE.
System.Runtime.InteropServices.Marshal.ReleaseComO bject(mainobj);
}


Here's my understanding of how com interop works. Dot net will create a
wrapper class around your com object. It will increment the ref count to 1
and keep it at 1 no matter how many references to the wrapper you create. It
will decrement it to zero if you call ReleaseComObject. You can still have
references to the wrapper after this but they will not function. If you do
the opposite and release all your references without calling
ReleaseComObject then the reference count will stay at 1 until the garbage
collector gets it. I'd say this is your problem in that the garbage
collector is causing your objects to be released in a different order to
what you'd normally expect. My guess would be that if you look in the code
for Release of your object you'll find the problem, maybe it references
another object there that has already been destroyed?

Michael
Dec 9 '05 #2
Michael,

Thanks for the response. Its an interesting idea. In my test program,
there is nothing at all in the objects except the one property for returning
the other object. It still happens even if I store a reference to the
parameter object in a local variable in C# (should maintain the reference
count). In the ATL object, if I do not release the object after firing the
event, it works fine except that the MainObj never gets released in C# or
any other language.

Jayme
Dec 9 '05 #3

"Jayme Pechan" <ja**********@whitefeld.com> wrote in message
news:O1*************@TK2MSFTNGP15.phx.gbl...
I have a ATL COM object that is loaded through Interop in a C# application.
The COM object fires an event and one of the parameters is another object
created inside the object. This object has a property on it that is also
another object.
private void pIObj_EventProc(MObj.MainObj pObj)
{
MObj.Obj mainobj = pObj.Obj;
// IF I DON'T DO THE NEXT LINE, THE APPLICATION HANGS AT SOME LATER
TIME
// THE UI DOESN'T EVEN UPDATE.
System.Runtime.InteropServices.Marshal.ReleaseComO bject(mainobj);
}

Property in ATL Object that returns another object...
HRESULT CMainObj::get_Obj(IObj** pVal)
{
IObj* pIObj= NULL;
CComObject<CObj>* pObj;
if (S_OK == CComObject<CObj>::CreateInstance(&pObj)
&& S_OK == pObj->QueryInterface(IID_IObj, (void**)&pIObj))
{
*pVal = pIObj;
return S_OK;
}
}

I don't fully understand interop but I'm thinking this could be the source
of my problem. Is there a way to make this reliable from the COM object
perspective? I'd rather not require the user to do the ReleaseComObject
in order to prevent their application from hanging at some random time.
Everything works correctly if the host is C++ or JScript or other such
languages.

Any help would be appreciated.


What kind of ATL object s this, a simple object or an AX control or...?
What kind of C# aplication is this? Where did you create the instance of the
COM object, which thread what's his threading attribute (STA/MTA)?
What exactly are you doing with the mainobj in your pIObj_EventProc?

Willy.

Dec 9 '05 #4
As it turns out, one of the objects being passed into the event handler was
created on a thread declared as Apartment model and I hadn't implemented the
required message loop. Since in this case I could not implement a message
loop because of another message processing loop I was doing, I simply
declared the thread as free threaded and it works fine. I have to worry
about accessability of the members on these objects but since they are all
read only members, that was not difficult.

"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:ep*************@tk2msftngp13.phx.gbl...

"Jayme Pechan" <ja**********@whitefeld.com> wrote in message
news:O1*************@TK2MSFTNGP15.phx.gbl...
I have a ATL COM object that is loaded through Interop in a C#
application. The COM object fires an event and one of the parameters is
another object created inside the object. This object has a property on
it that is also another object.
private void pIObj_EventProc(MObj.MainObj pObj)
{
MObj.Obj mainobj = pObj.Obj;
// IF I DON'T DO THE NEXT LINE, THE APPLICATION HANGS AT SOME LATER
TIME
// THE UI DOESN'T EVEN UPDATE.
System.Runtime.InteropServices.Marshal.ReleaseComO bject(mainobj);
}

Property in ATL Object that returns another object...
HRESULT CMainObj::get_Obj(IObj** pVal)
{
IObj* pIObj= NULL;
CComObject<CObj>* pObj;
if (S_OK == CComObject<CObj>::CreateInstance(&pObj)
&& S_OK == pObj->QueryInterface(IID_IObj, (void**)&pIObj))
{
*pVal = pIObj;
return S_OK;
}
}

I don't fully understand interop but I'm thinking this could be the
source of my problem. Is there a way to make this reliable from the COM
object perspective? I'd rather not require the user to do the
ReleaseComObject in order to prevent their application from hanging at
some random time. Everything works correctly if the host is C++ or
JScript or other such languages.

Any help would be appreciated.


What kind of ATL object s this, a simple object or an AX control or...?
What kind of C# aplication is this? Where did you create the instance of
the COM object, which thread what's his threading attribute (STA/MTA)?
What exactly are you doing with the mainobj in your pIObj_EventProc?

Willy.

Jan 13 '06 #5

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

Similar topics

2
9149
by: Jon Davis | last post by:
The garbage handler in the .NET framework is handy. When objects fall out of scope, they are automatically destroyed, and the programmer doesn't have to worry about deallocating the memory space...
6
2780
by: Catherine Jones | last post by:
Hi all, we need urgent help in a matter. We are trying to pass a COM object from the client to server and are facing some problems in the same. We've our client in C# as well as the Server...
5
1636
by: blue | last post by:
We often get connection pooling errors saying that there are no available connections in the pool. I think the problem is that we are passing around open readers all over the place. I am...
6
5973
by: MSDNAndi | last post by:
Hi, I get the following warning: "Possibly incorrect assignment to local 'oLockObject' which is the argument to a using or lock statement. The Dispose call or unlocking will happen on the...
4
1576
by: Dave A | last post by:
I am developing a somewhat complex component at the moment and coincidently I am also reading the Framework Design Guidelines book. After reading the section about event raising I have re-written...
16
2869
by: anonymous.user0 | last post by:
The way I understand it, if I have an object Listener that has registered as a listener for some event Event that's produced by an object Emitter, as long as Emitter is still allocated Listener...
12
2182
by: cj | last post by:
I would like to have menu items a main menu bar that represent the days of the week. When you click on them they alternate from checked to unchecked. Right now I have 7 subs that look like this...
5
2740
by: Daniel | last post by:
Hey guys When you hook an event (c# 2.0 syntax): myEvent += MyMethodToFire; You need to also unsubscribe it to avoid a resource leak so that the object it is in gets garbage collected like so...
10
3901
by: amazon | last post by:
Our vender provided us a web service: 1xyztest.xsd file... ------------------------------------ postEvent PostEventRequest ------------------------------------- authetication authentication...
11
3217
by: MikeT | last post by:
This may sound very elementary, but can you trap when your object is set to null within the object? I have created a class that registers an event from an object passed in the constructor. When...
0
7080
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
6950
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
7103
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
6811
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
7209
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...
1
4841
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4527
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1355
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 ...
0
234
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.