[Mono-dev] StackOverflow on System.Delegate.Equals

kralu at poczta.onet.pl kralu at poczta.onet.pl
Mon Apr 4 17:54:07 EDT 2011


On Mon, 2011-04-04 22:41:47 nekresh <nekresh at gmail.com> wrote:
> On Mon, Apr 4, 2011 at 10:37 PM,  <kralu at poczta.onet.pl> wrote:
> > On Mon, 2011-04-04 22:03:47 Gonzalo Paniagua Javier <gonzalo.mono at gmail.com> wrote:
> >> On Mon, 2011-04-04 at 20:48 +0100, Alan wrote:
> >> > Aren't event handler methods emitted with a [synchronized] attribute
> >> > by default which would prevent this issue? You can check by
> >> > disassembling the IL and seeing if its there.
> >>
> >> They are synchronized as long as you don't replace the default
> >> add/remove with your own code.
> >
> > AFAIK you cannot inherit from the Delegate/MulticastDelegate class. So how can I replace the default add/remove code? Anyway I'm almost sure that case has no place in my code but I have to take a look on 3rd part libraries.  Thanks in advance for any help.
> >
> public event MyType MyEvent {
>   add { }
>   remove { }
> }

Ok, thanks. I found in Telerik's code a few places where code looks like:
		public event XmlHttpPanelEventHandler ServiceRequest
		{
			add
			{
				base.Events.AddHandler(WebServiceRequestEvent, value);
			}
			remove
			{
				base.Events.RemoveHandler(WebServiceRequestEvent, value);
			}
		}

The base.Events is a property of the WebControl class. Maybe that's the problem? 
I've also took a look on a code generated for a "default" event handler (from my class):

internal event EventHandler<MenuItemClickedEventArgs> ItemClicked
{
    add
    {
        EventHandler<MenuItemClickedEventArgs> handler2;
        EventHandler<MenuItemClickedEventArgs> itemClicked = this.ItemClicked;
        do
        {
            handler2 = itemClicked;
            EventHandler<MenuItemClickedEventArgs> handler3 = (EventHandler<MenuItemClickedEventArgs>) Delegate.Combine(handler2, value);
            itemClicked = Interlocked.CompareExchange<EventHandler<MenuItemClickedEventArgs>>(ref this.ItemClicked, handler3, handler2);
        }
        while (itemClicked != handler2);
    }
    remove
    {
        EventHandler<MenuItemClickedEventArgs> handler2;
        EventHandler<MenuItemClickedEventArgs> itemClicked = this.ItemClicked;
        do
        {
            handler2 = itemClicked;
            EventHandler<MenuItemClickedEventArgs> handler3 = (EventHandler<MenuItemClickedEventArgs>) Delegate.Remove(handler2, value);
            itemClicked = Interlocked.CompareExchange<EventHandler<MenuItemClickedEventArgs>>(ref this.ItemClicked, handler3, handler2);
        }
        while (itemClicked != handler2);
    }
}


Kind regards,
Marcin






More information about the Mono-devel-list mailing list