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

  • Home
  • SEARCH
  • 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 624601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:13:05+00:00 2026-05-13T19:13:05+00:00

As seen in this question: Raising C# events with an extension method – is

  • 0

As seen in this question:
Raising C# events with an extension method – is it bad?

I’m thinking of using this extension method to safely raise an event:

public static void SafeRaise(this EventHandler handler, object sender, EventArgs e)
{
    if (handler != null)
        handler(sender, e);
}

But Mike Rosenblum raise this concern in Jon Skeet’s answer:

You guys need to add the
[MethodImpl(MethodImplOptions.NoInlining)]
attribute to these extension methods
or else your attempt to copy the
delegate to a temporary variable could
be optimized away by the JITter,
allowing for a null reference
exception.

I did some test in Release mode to see if I could get a race condition when the extension method is not marked with NoInlining:

int n;
EventHandler myListener = (sender, e) => { n = 1; };
EventHandler myEvent = null;

Thread t1 = new Thread(() =>
{
    while (true)
    {
        //This could cause a NullReferenceException
        //In fact it will only cause an exception in:
        //    debug x86, debug x64 and release x86
        //why doesn't it throw in release x64?
        //if (myEvent != null)
        //    myEvent(null, EventArgs.Empty);

        myEvent.SafeRaise(null, EventArgs.Empty);
    }
});

Thread t2 = new Thread(() =>
{
    while (true)
    {
        myEvent += myListener;
        myEvent -= myListener;
    }
});

t1.Start();
t2.Start();

I ran the test for a while in Release mode and never had a NullReferenceException.

So, was Mike Rosenblum wrong in his comment and method inlining cannot cause race condition?

In fact, I guess the real question is, will SaifeRaise be inlined as:

while (true)
{
    EventHandler handler = myEvent;
    if (handler != null)
        handler(null, EventArgs.Empty);
}

or

while (true)
{
    if (myEvent != null)
        myEvent(null, EventArgs.Empty);
}
  • 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-05-13T19:13:05+00:00Added an answer on May 13, 2026 at 7:13 pm

    The problem wouldn’t have been inlining the method – it would have been the JITter doing interesting things with memory access whether or not it was inlined.

    However, I don’t believe it is an issue in the first place. It was raised as a concern a few years back, but I believe that was regarded as a flawed reading of the memory model. There’s only one logical “read” of the variable, and the JITter can’t optimise that away such that the value changes between one read of the copy and the second read of the copy.

    EDIT: Just to clarify, I understand exactly why this is causing a problem for you. You’ve basically got two threads modifying the same variable (as they’re using captured variables). It’s perfectly possible for the code to occur like this:

    Thread 1                      Thread 2
    
                                  myEvent += myListener;
    
    if (myEvent != null) // No, it's not null here...
    
                                  myEvent -= myListener; // Now it's null!
    
    myEvent(null, EventArgs.Empty); // Bang!
    

    This is slightly less obvious in this code than normally, as the variable is a captured variable rather than a normal static/instance field. The same principle applies though.

    The point of the safe raise approach is to store the reference in a local variable which can’t be modified from any other threads:

    EventHandler handler = myEvent;
    if (handler != null)
    {
        handler(null, EventArgs.Empty);
    }
    

    Now it doesn’t matter whether thread 2 changes the value of myEvent – it can’t change the value of handler, so you won’t get a NullReferenceException.

    If the JIT does inline SafeRaise, it will be inlined to this snippet – because the inlined parameter ends up as a new local variable, effectively. The problem would only be if the JIT incorrectly inlined it by keeping two separate reads of myEvent.

    Now, as to why you only saw this happen in debug mode: I suspect that with the debugger attached, there’s far more room for threads to interrupt each other. Possibly some other optimisation occurred – but it didn’t introduce any breakage, so that’s okay.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen this question , and using its method throws up an error
I have seen this question about deploying to WebSphere using the WAS ant tasks.
I have seen this question and a number of blog posts related to using
I've seen this question asked in .asp threads but I'm not using .asp so
I have seen this question here, and was wondering if the same method of
I've seen this question: Class design with vector as a private/public member? , but
I've already seen this question . It suggests that AMQP PECL extension is not
I'm raising the question as seen from this Code Academy lesson . Thanks in
I have seen this question and its answers and they clear up some of
I've seen this question asked often, but none of the proposed solutions seem to

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.