473,549 Members | 2,862 Online
Bytes | Software Development & Data Engineering Community
+ 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.Releas eComObject(main obj);
}

Property in ATL Object that returns another object...
HRESULT CMainObj::get_O bj(IObj** pVal)
{
IObj* pIObj= NULL;
CComObject<CObj >* pObj;
if (S_OK == CComObject<CObj >::CreateInstan ce(&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 ReleaseComObjec t 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 3881
"Jayme Pechan" <ja**********@w hitefeld.com> wrote in message
news:O1******** *****@TK2MSFTNG P15.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.Releas eComObject(main obj);
}


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 ReleaseComObjec t. 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
ReleaseComObjec t 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**********@w hitefeld.com> wrote in message
news:O1******** *****@TK2MSFTNG P15.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.Releas eComObject(main obj);
}

Property in ATL Object that returns another object...
HRESULT CMainObj::get_O bj(IObj** pVal)
{
IObj* pIObj= NULL;
CComObject<CObj >* pObj;
if (S_OK == CComObject<CObj >::CreateInstan ce(&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 ReleaseComObjec t
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******** *****@tk2msftng p13.phx.gbl...

"Jayme Pechan" <ja**********@w hitefeld.com> wrote in message
news:O1******** *****@TK2MSFTNG P15.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.Releas eComObject(main obj);
}

Property in ATL Object that returns another object...
HRESULT CMainObj::get_O bj(IObj** pVal)
{
IObj* pIObj= NULL;
CComObject<CObj >* pObj;
if (S_OK == CComObject<CObj >::CreateInstan ce(&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
ReleaseComObjec t 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
9158
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 for those objects. In fact, all the programmer has to worry about is the total sum of objects loaded into RAM at any known point. Memory leaks are not...
6
2784
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 in C# and we're using remoting for client to server communication.
5
1643
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 planning on changing this in our code and I expect this to fix our problem. We have our connection pooling set to the default number of connections...
6
5980
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 original value of the local." My code is: using System; using System.Collections.Generic;
4
1584
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 the way my component raises events to follow the Framework Design Guides verbatim; ie (object sender, EventArgs (or some subclass there of) e). ...
16
2882
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 will stay alive. Is this correct? If this is correct, I've got a problem. Let's say I've got an object Customer that has an PurchaseList...
12
2192
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 one: Private Sub SunMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SunMenuItem.Click If SunMenuItem.Checked...
5
2751
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 : myEvent -= MyMethodToFire; That's all fine, but when you use visual studio to create events for objects it never creates an unsubscribing...
10
3906
by: amazon | last post by:
Our vender provided us a web service: 1xyztest.xsd file... ------------------------------------ postEvent PostEventRequest ------------------------------------- authetication authentication eventname string source string ID string
11
3233
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 my object is destroyed, I want my object to un-register this event. If I don't then the object would never be destroyed until the object I passed...
0
7518
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7446
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7715
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7956
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7808
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5368
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3498
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1935
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 we have to send another system
1
1057
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.