Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / September 2003

Tip: Looking for answers? Try searching our database.

Unadvise problem with managed event source

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
David Shepherd - 08 Sep 2003 16:55 GMT
Hi

I have a managed class written in C# which fires events to an
unmanaged dispinterface sink. This setup is working fine, except when
I try to disconnect from the event source the reference count on the
sink is not released straight away and remains the same until the
program is just about to exit. This is causing me big problems since
the sink is part of a reusable component which can be reset and used
many times over, but now is not possible with managed objects.

Is there a way to force the connection point to release my interface
straight away as in normal COM, or maybe some other workaround for
this?

Thanks for all information

David Shepherd
Ying-Shen Yu[MS] - 09 Sep 2003 10:43 GMT
Hi dave,
   Sorry, I'm not fully understand your problem, how you disconnect the
dispinterface?
Well, If you want to manually addref/release the component, maybe the
Marshal class will help you
there is  AddRef/Release methods let you manage the reference counting on
your own. for detail usage
you may refer to the following link in MSDN.
ms-help://MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemruntimeinteropservic
esmarshalclassreleasetopic.htm
If you still have problems on it, please reply me with more information,
thanks!

Kind regards,

Ying-Shen Yu [MS]
Microsoft Support Engineer

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use.  2001 Microsoft Corporation. All rights
reserved.
--------------------
| From: dave@daveshep.com (David Shepherd)
| Newsgroups: microsoft.public.dotnet.framework.interop
[quoted text clipped - 7 lines]
| Content-Transfer-Encoding: 8bit
| X-Trace: posting.google.com 1063036559 1615 127.0.0.1 (8 Sep 2003
15:55:59 GMT)
| X-Complaints-To: groups-abuse@google.com
| NNTP-Posting-Date: 8 Sep 2003 15:55:59 GMT
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
e.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!sn-xit-03!sn-xit-06!sn-
xit-09!supernews.com!postnews1.google.com!not-for-mail
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.interop:18587
| X-Tomcat-NG: microsoft.public.dotnet.framework.interop
[quoted text clipped - 16 lines]
|
| David Shepherd
David Shepherd - 09 Sep 2003 15:23 GMT
Hi Ying-Shen

I am disconnecting from the managed event source by calling
IConnectionPoint::Unadvise from within unmanaged code. If the event
source were a typical COM component, it would decrement the reference
count on the sink immediately as part of the Unadvise process, but for
some reason, the managed component holds the reference for a long time
after Unadvise has been called.

According to the MSDN Library
When an advisory connection is terminated, the connection point calls
the Release method on the pointer that was saved for the connection
during the IConnectionPoint::Advise method. This Release reverses the
AddRef that was performed during the IConnectionPoint::Advise when the
connection point calls the advisory sink's QueryInterface.
Which seems to suggest that the sink should be released straight away?

Thank you for the suggestion of artificially adjusting the reference
count on the sink by calling Release manually, unfortunately this
would not work in my case though since it will cause the sink to be
deleted prematurely and give problems when the event source finally
gets around to calling Release its self.

If you have any other ideas on this problem, I would really appreciate
this.

TIA
David Shepherd.

> Hi dave,
>     Sorry, I'm not fully understand your problem, how you disconnect the
[quoted text clipped - 56 lines]
> |
> | David Shepherd
Ying-Shen Yu[MS] - 10 Sep 2003 14:28 GMT
Hi dave,
   Thanks for your detail explaination, My understanding now is you have
an managed control and it implemented the IConntectionPoint interface( by
deriving the UCOMIConnectionPoint right?). you expose this .NET Control as
an COM Component and use it in an unmanaged code. When you called the
UnAdvise method
the reference count didn't decreased untile you closed your program. If I
was wrong ,please let me know.
  But to do research on this problem, I'd like to know more on the detail
implementation, such as the implementation of your IConnectionPoint and
some code snnipet of your unmanaged code shows how you use the
IConnectionPoint interface. If you can provide a small example that would
be much more helpful.
  Thanks a lot!

Kind regards,

Ying-Shen Yu [MS]
Microsoft Support Engineer

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use.  2001 Microsoft Corporation. All rights
reserved.
--------------------
| From: dave@daveshep.com (David Shepherd)
| Newsgroups: microsoft.public.dotnet.framework.interop
[quoted text clipped - 4 lines]
| Message-ID: <82349813.0309090623.54870791@posting.google.com>
| References: <82349813.0309080755.6ce37d5d@posting.google.com>
<u5sGebrdDHA.2536@cpmsftngxa06.phx.gbl>
| NNTP-Posting-Host: 81.129.36.31
| Content-Type: text/plain; charset=ISO-8859-1
| Content-Transfer-Encoding: 8bit
| X-Trace: posting.google.com 1063117408 17458 127.0.0.1 (9 Sep 2003
14:23:28 GMT)
| X-Complaints-To: groups-abuse@google.com
| NNTP-Posting-Date: 9 Sep 2003 14:23:28 GMT
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
e.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!sn-xit-03!sn-xit-01!sn-
xit-09!supernews.com!postnews1.google.com!not-for-mail
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.interop:18632
| X-Tomcat-NG: microsoft.public.dotnet.framework.interop
[quoted text clipped - 36 lines]
| > your own. for detail usage
| > you may refer to the following link in MSDN.

ms-help://MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemruntimeinteropservic
| > esmarshalclassreleasetopic.htm
| > If you still have problems on it, please reply me with more information,
[quoted text clipped - 24 lines]
| > | NNTP-Posting-Date: 8 Sep 2003 15:55:59 GMT
| > | Path:

cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin

e.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!sn-xit-03!sn-xit-06!sn-
| > xit-09!supernews.com!postnews1.google.com!not-for-mail
| > | Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.interop:18587
[quoted text clipped - 17 lines]
| > |
| > | David Shepherd
David Shepherd - 10 Sep 2003 17:38 GMT
Hi Ying-Shen

I will try and describe my implementation in as much detail as
possible if this will be of help.

On the managed side of things, I have declared an interface which will
be the source of events. I am using the InterfaceType attribute to
mark the interface as a dispatch interface, also the DispId attribute
is used on each method so that GetIDsOfNames is not called on the sink
for each event invocation.

[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IClass1Events
{
    [DispId(1)]
    void TextChanged();
}

The class which uses this interface is what I was referring to in the
other posts as the managed event source. I have used the
ComSourceInterfaces attribute on this class to indicate that the
IClass1Events interface is the COM source interface. An instance of
this class is created when the application first starts up and remains
alive until the application closes.

public delegate void TextChangedEvent();

[ComSourceInterfaces("CSDemo.IClass1Events")]
public class Class1
{
    public event TextChangedEvent TextChanged;

    public string Text
    {
        ...
        set
        {
            ...
            TextChanged();
        }
    }
}

The unmanaged component is an ActiveX control which I have placed onto
a Windows Form, this generated a RCW that I am using to call methods
on the ActiveX control from within managed code. One of the methods of
the ActiveX control takes an IDispatch pointer to an automation object
and then starts sinking all events from this object. The RCW expects
an object to be passed rather than an IDpsiatch pointer and so I can
easily pass an instance of my class (Class1) instead.

Inside the ActiveX control, the connection to the event source is made
using AtlAdvise (which calls IConnectionPoint::Advise internally).

if(!SUCCEEDED(AtlAdvise(m_spSourceDispatch,
    (IUnknown*)this,m_SourceEventInterfaceGuid,&m_Cookie)))
{
    *This error never occurs.
}

m_spSourceDispatch = the IDispatch interface for Class1.
this = the event sink.
m_SourceEventInterfaceGuid = the GUID of IClass1Events (obtained
dynamically from type information).
m_Cookie = the connection cookie.

To disconnect from the event source, AtlUnadvise is used (which calls
IConnectionPoint::Undvise internally).

if(!SUCCEEDED(AtlUnadvise(
m_spSourceDispatch,m_SourceEventInterfaceGuid,m_Cookie)))
{
    *This error never occurs.
}

If I set a breakpoint on the disconnection line, I can see the
reference count of the sink remains exactly the same after AtlUnadvise
has been called as it was before, but for normal COM objects, the
reference count is decremented as part of the unadvise call. The
reference count of the sink does finally get decremented, but only
when the application exits (maybe because of garbage collection?).

I hope this is enough information, Ying-Shen, if you need anymore,
then I can post some additional code.

Many Thanks
David Shepherd.

> Hi dave,
>     Thanks for your detail explaination, My understanding now is you have
[quoted text clipped - 143 lines]
> | > |
> | > | David Shepherd
Ying-Shen Yu[MS] - 11 Sep 2003 14:43 GMT
Hi David,
   Thanks for your detail information, and I think I found some clue in
the MSDN,
you may find the following words,
"Each RCW maintains a cache of interface pointers on the COM object it
wraps and releases its reference on the COM object when the RCW is no
longer needed. The runtime performs garbage collection on the RCW."
for more information you may refer to this link
ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconruntimecallablewrapper.h
tm
Now I'm doing research to find if there is an work around, I'll update you
as soon as possible.
Thanks!

Best regards,

Ying-Shen Yu [MS]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: dave@daveshep.com (David Shepherd)
| Newsgroups: microsoft.public.dotnet.framework.interop
[quoted text clipped - 4 lines]
| Message-ID: <82349813.0309100838.7502903c@posting.google.com>
| References: <82349813.0309080755.6ce37d5d@posting.google.com>
<u5sGebrdDHA.2536@cpmsftngxa06.phx.gbl>
<82349813.0309090623.54870791@posting.google.com>
<9#Y5195dDHA.1120@cpmsftngxa06.phx.gbl>
| NNTP-Posting-Host: 81.129.59.76
| Content-Type: text/plain; charset=ISO-8859-1
| Content-Transfer-Encoding: 8bit
| X-Trace: posting.google.com 1063211921 11949 127.0.0.1 (10 Sep 2003
16:38:41 GMT)
| X-Complaints-To: groups-abuse@google.com
| NNTP-Posting-Date: 10 Sep 2003 16:38:41 GMT
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
e.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!sn-xit-03!sn-xit-01!sn-
xit-09!supernews.com!postnews1.google.com!not-for-mail
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.interop:18675
| X-Tomcat-NG: microsoft.public.dotnet.framework.interop
[quoted text clipped - 128 lines]
| > | NNTP-Posting-Date: 9 Sep 2003 14:23:28 GMT
| > | Path:

cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin

e.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!sn-xit-03!sn-xit-01!sn-
| > xit-09!supernews.com!postnews1.google.com!not-for-mail
| > | Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.interop:18632
[quoted text clipped - 43 lines]
| > | >
| >  
ms-help://MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemruntimeinteropservic
| > | > esmarshalclassreleasetopic.htm
| > | > If you still have problems on it, please reply me with more
[quoted text clipped - 29 lines]
| > | >
| >  
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin

| >  
e.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!sn-xit-03!sn-xit-06!sn-
| > | > xit-09!supernews.com!postnews1.google.com!not-for-mail
| > | > | Xref: cpmsftngxa06.phx.gbl
[quoted text clipped - 18 lines]
| > | > |
| > | > | David Shepherd
Liju Thomas [MS] - 12 Sep 2003 23:02 GMT
Hi Dave,

Can you send a repro as an attachment so that we can reproduce the
behaviour you are seeing and troubleshoot it?

thanks,
Liju Thomas

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use.

?2001 Microsoft Corporation. All rights reserved.

--------------------
| From: dave@daveshep.com (David Shepherd)
| Newsgroups: microsoft.public.dotnet.framework.interop
[quoted text clipped - 4 lines]
| Message-ID: <82349813.0309100838.7502903c@posting.google.com>
| References: <82349813.0309080755.6ce37d5d@posting.google.com>
<u5sGebrdDHA.2536@cpmsftngxa06.phx.gbl>
<82349813.0309090623.54870791@posting.google.com>
<9#Y5195dDHA.1120@cpmsftngxa06.phx.gbl>
| NNTP-Posting-Host: 81.129.59.76
| Content-Type: text/plain; charset=ISO-8859-1
| Content-Transfer-Encoding: 8bit
| X-Trace: posting.google.com 1063211921 11949 127.0.0.1 (10 Sep 2003
16:38:41 GMT)
| X-Complaints-To: groups-abuse@google.com
| NNTP-Posting-Date: 10 Sep 2003 16:38:41 GMT
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
e.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!sn-xit-03!sn-xit-01!sn-
xit-09!supernews.com!postnews1.google.com!not-for-mail
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.interop:18675
| X-Tomcat-NG: microsoft.public.dotnet.framework.interop
[quoted text clipped - 128 lines]
| > | NNTP-Posting-Date: 9 Sep 2003 14:23:28 GMT
| > | Path:

cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin

e.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!sn-xit-03!sn-xit-01!sn-
| > xit-09!supernews.com!postnews1.google.com!not-for-mail
| > | Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.interop:18632
[quoted text clipped - 43 lines]
| > | >
| >  
ms-help://MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemruntimeinteropservic
| > | > esmarshalclassreleasetopic.htm
| > | > If you still have problems on it, please reply me with more
[quoted text clipped - 29 lines]
| > | >
| >  
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin

| >  
e.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!sn-xit-03!sn-xit-06!sn-
| > | > xit-09!supernews.com!postnews1.google.com!not-for-mail
| > | > | Xref: cpmsftngxa06.phx.gbl
[quoted text clipped - 18 lines]
| > | > |
| > | > | David Shepherd

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use.

?2001 Microsoft Corporation. All rights reserved.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.