473,692 Members | 3,149 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 3889
"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
9179
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 a problem. .... So one would like to think. The reality is that delegates and event...
6
2794
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
1646
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 open. We probably have about 3-7 users concurrently using our web site. So, the problem isn't that we...
6
5992
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
1593
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). The thing that was not explained is why should I need to cast the sender to 'object' when I know...
16
2894
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 (Collection) of Purchase objects. Now, these Purchase objects were pulled from a datasource Datasource. The...
12
2204
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 Then SunMenuItem.Checked = False Else SunMenuItem.Checked = True
5
2770
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 reference, so is it puting in resource leaks? Or is this being cleared somewhere that i am not seeing?
10
3928
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
3254
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 in the constructor is destroyed. I have implemented a Dispose(), Dispose(bool), and ~Finalize...
0
8611
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8970
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8814
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8812
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7642
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4565
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2989
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
2
2244
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1966
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.