Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8909429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:25:44+00:00 2026-06-15T03:25:44+00:00

I saw this sample code in SO that was saying one practice is bad

  • 0

I saw this sample code in SO that was saying one practice is bad and the other one is good. But I don’t understand why?
As a matter of fact I am getting that famous RCW COM object error and that post was saying this could be a reason.

public class SomeClass
{
    private Interop.ComObjectWrapper comObject;
    private event ComEventHandler comEventHandler;

    public SomeClass()
    {
        comObject = new Interop.ComObjectWrapper();

        // NO - BAD!
        comObject.SomeEvent += new ComEventHandler(EventCallback);

        // YES - GOOD!
        comEventHandler = new ComEventHandler(EventCallback);
        comObject.SomeEvent += comEventHandler
    }

    public void EventCallback()
    {
        // DO WORK
    }

}

Edit: here is the link to the source: COM object that has been separated from its underlying RCW cannot be used

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T03:25:46+00:00Added an answer on June 15, 2026 at 3:25 am

    I think those two code snippets are the same and we don’t have any issues with strong/weak references here.

    Background

    First of all, if our Interop.ComObjectWrapper provides CLR event (i.e. event that stores event handlers in delegates) we’ll definitely got a strong reference from ComObjectWrapper to our object.

    Any delegate contains two parts: Target of type object and Method Pointer to the particular method. If Target is null than callback points to the static method.

    Its impossible to have a delegate with Target of type WeakReference. There is so called Weak Event Pattern but it implements on top of EventManager instead of plain delegates.

    Storing event handler in the field will not help. Part 1

    Internal event implementation means that after subscribing to the event:

    comObject.SomeEvent += EventCallback;
    

    comObject object implicitely holds an strong reference to the SomeClass object. And this true regardless of what kind of subscribing technique your are using and whether ComObject is a COM object wrapper or not.

    Subscribing to the event adds implicit dependency between two objects in terms of lifetime. That’s why the most common memory leak in .NET world caused by subscribing to events of the long-lived objects. Event subscriber will not die till event holder accessible in the application.

    Storing event handler in the field will not help. Part 2

    But event if my assumption is not true and ComObjectWrapper provides some notion of Weak Event Pattern, saving event handler in the field will not help any way.

    Lets recap what event keyword mean:

    private event ComEventHandler comEventHandler;
    ... 
    comEventHandler = new ComEventHandler(EventCallback);
    

    Saving callback in current field (and basically we can treat private events as a simple delegate fields) will not change existing behavior.

    We already know that delegate is a simple object that stores reference to the Target object (i.e. SomeClass object) and a method (i.e. public void EventCallBack()). This means that storing additional delegate in field adds additional reference to the SomeClass from the SomeClass itself.

    Basically, storing event handler in the field semantically equivalent to storing additional reference in SomeClass:

    private SomeClass someClass;

    public SomeClaas()
    {
    // This is basically the same as storing delegate
    // in the comEventHandler field
    someClass = this;
    }

    Storing strong reference in the SomeClass will not prolong lifetime of the current object. And this means that if ComObjectWrapper will not hold a strong reference to the SomeClass object storing event handler in the comEventHandler will not prolong SomeClass’s lifetime and will not prevent SomeClass from garbage collection.

    Conclusion

    Storing event handler in the private field will not prolong object’s lifetime and will not prevent it from garbage collection anyway.

    That’s why there is no difference between following code snippets in terms of object lifetime:

        // GOOD!
        comObject.SomeEvent += new ComEventHandler(EventCallback);
    
        // EVEN BETTER!
        comObject.SomeEvent += EventCallback;
    
        // NOT GOOD, BECAUSE WAN'T HELP!
        comEventHandler = new ComEventHandler(EventCallback);
        comObject.SomeEvent += comEventHandler
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I saw that this was used in sample code to accept all the warnings
I saw this C# using statement in a code example: using StringFormat=System.Drawing.StringFormat; What's that
I'm searching for a hmac-sha1 code sample in objective-c I saw this sample and
In one of the WCF tutorials, I saw the following sample code: Dim service
I saw this code sample without any explanation: var xhr = new XMLHttpRequest(); xhr.open('GET',
I was looking through the docs for instance_variable_set and saw that the sample code
I have this chunk of code, one sample is $dispatch='TC_12' : foreach($this->dispatch as $dispatch){
I saw this as an example on MDN and didn't understand why this was
I saw this in a answer by Marc Gravell, and I just don't see
I hate that google can not search for symbols. I saw this in some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.