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 239093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:33:28+00:00 2026-05-11T20:33:28+00:00

I am getting a NullReferenceException when running my multi-threaded application, but only when I

  • 0

I am getting a NullReferenceException when running my multi-threaded application, but only when I run in Release mode outside of the debugger. The stack trace gets logged, and it always points to the same function call. I put several logging statements in the function to try to determine how far it would get, and every statement gets logged, including one on the last line of the function. What is interesting is that when the NullReferenceException occurs, the statement after the function call does not get logged:

    // ...
    logger.Log( "one" );  // logged
    Update( false );
    logger.Log( "eleven" );  // not logged when exception occurs
}

private void Update( bool condition )
{
    logger.Log( "one" );  // logged
    // ...  
    logger.Log( "ten" );  // logged, even when exception occurs
}

The exception does not occur every time the function is called. Is it possible that the stack is being corrupted either before or during execution of the function such that the return address is lost, resulting in the null reference? I didn’t think that sort of thing was possible under .NET, but I guess stranger things have happened.

I tried replacing the call to the function with the contents of the function, so everything happens inline, and the exception then occurs on a line that looks like this:

foreach ( ClassItem item in classItemCollection )

I have verified through logging that the “classItemCollection” is not null, and I also tried changing the foreach to a for in case the IEnumerator was doing something funny, but the exception occurs on the same line.

Any ideas on how to investigate this further?

Update: Several responders have suggested possible solutions having to do with making sure the logger isn’t null. To be clear, the logging statements were added for debugging purposes after the exception started happening.

  • 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-11T20:33:28+00:00Added an answer on May 11, 2026 at 8:33 pm

    I found my null reference. Like Fredrik and micahtan suggested, I didn’t provide enough information for the community to find a solution, so I figured I should post what I found just to put this to rest.

    This is a representation of what was happening:

    ISomething something = null;
    
    //...
    
    // the Add method returns a strong reference to an ISomething
    // that it creates.  m_object holds a weak reference, so when
    // "this" no longer has a strong reference, the ISomething can
    // be garbage collected.
    something = m_object.Add( index );
    
    // the Update method looks at the ISomethings held by m_object.
    // it obtains strong references to any that have been added,
    // and puts them in m_collection;
    Update( false );
    
    // m_collection should hold the strong reference created by 
    // the Update method.
    // the null reference exception occurred here
    something = m_collection[ index ];
    
    return something;
    

    The problem turned out to be my use of the “something” variable as a temporary strong reference until the Update method obtained a permanent one. The compiler, in Release mode, optimizes away the “something = m_object.Add();” assignment, since “something” isn’t used until it is assigned again. This allowed the ISomething to be garbage collected, so it no longer existed in m_collection when I tried to access it.

    All I had to do was ensure that I held a strong reference until after the call to Update.

    I am doubtful that this will be of any use to anyone, but in case anyone was curious, I didn’t want to leave this question unanswered.

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

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can get an array of keys with allKeys (undefined… May 11, 2026 at 11:49 pm
  • Editorial Team
    Editorial Team added an answer Ok so I figured it out myself (sorta) so I… May 11, 2026 at 11:49 pm
  • Editorial Team
    Editorial Team added an answer There is nothing wrong with running (limited) code in the… May 11, 2026 at 11:49 pm

Related Questions

We started getting the 'Object reference not set to an instance of an object'
I am trying to use Microsoft's CLRProfiler to profile my fairly large app, and
I am doing a little debugging, and so I want to log the eventArgs
I'm just trying to do some simple validation in MVC RC and am getting

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.