none
Handling COM events in .NET - Lifetime of event helpers RRS feed

  • Question

  • Hi folks,

    I am having trouble consuming events of COM objects from C# (the events source is a UI library I am developing).

    I have found that the automatically generated event handler helpers get garbage-collected unless I store objects I want to subscribe to in member variables of my class.

    e.g.

    class MyClass
    {
        private ApplicationType myApplication = ApplicationType.GetApplicationFactoryMethod();
        rxButton myButton2;
    
    
        public MyClass()
        {
            // ... variable theGroup returned from a method of myApplication
    
            // This event handler gets garbage collected
            rxButton myButton1 = theGroup.buttons.add(new rxButton
            {
                label = "Button1"
            });
            myButton1.OnActionEvent += myButton1_OnActionEvent;
    
            // This survives the GC collection
            myButton2 = theGroup.buttons.add(new rxButton
            {
                label = "Button2"
            });
            myButton2.OnActionEvent += myButton2_OnActionEvent;
    
    
            GC.Collect();
            GC.WaitForPendingFinalizers();
    
        }
    
    }

    As indicated in the comments, the event handler helper for myButton1 gets  collected, while the event helper for myButton2 survives the GC collection call.

    What I find somewhat confusing is that both variables myButton1 and myButton2 eventually refer to objects that survive the ctor (because they get added to the 'application' obj and referenced through it), but I suppose it all has to do with the lifetime of the CCW that's being generated rather than the lifetime of the actual object.

    Can you please verify this behaviour? Is there something I can do on the library/ PIA generation side to change this? (This hurts the composability of the API somewhat)

    As an alternative, the pattern below seems to improve things slightly. Is that something you would consider acceptable?

    class MyClass
    {
        private ApplicationType myApplication = ApplicationType.GetApplicationFactoryMethod();
        rxButton myButton2;
    
        private HashSet<object> _eventControlsList = new HashSet<object>();
        T PersistObj<T>(T obj)
        {
            _eventControlsList.Add((object)obj);
            return obj;
        }
    
    
        public MyClass()
        {
            // ... variable theGroup returned from a method of myApplication
    
            // This event handler gets garbage collected
            rxButton myButton1 = theGroup.buttons.add(new rxButton
            {
                label = "Button1"
            });
            PersistObj(myButton1).OnActionEvent += myButton1_OnActionEvent;
    
            // This survives the GC collection
            rxButton myButton2 = theGroup.buttons.add(new rxButton
            {
                label = "Button2"
            });
            PersistObj(myButton2).OnActionEvent += myButton2_OnActionEvent;
    
    
            GC.Collect();
            GC.WaitForPendingFinalizers();
    
        }
    
    }

    Many thanks,

    Yiannis

    Thursday, January 24, 2013 3:15 PM

Answers

  • Hi Yiannis,

    Why you didn't declare "

    rxButton myButton1

    "

    in the class members?

    >>Is that something you would consider acceptable?

    In my opinion, it is totally acceptable, generally, we just declare it, without a collection. But that doesn't means the collection is bad.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, January 25, 2013 3:31 AM
    Moderator

All replies

  • Hi Yiannis,

    Why you didn't declare "

    rxButton myButton1

    "

    in the class members?

    >>Is that something you would consider acceptable?

    In my opinion, it is totally acceptable, generally, we just declare it, without a collection. But that doesn't means the collection is bad.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, January 25, 2013 3:31 AM
    Moderator
  • Thanks for confirming Mike.

    Regards,

    Yiannis

    Friday, January 25, 2013 3:15 PM