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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:33:38+00:00 2026-05-23T15:33:38+00:00

So I encountered a strange issue today – I had a simple creation of

  • 0

So I encountered a strange issue today – I had a simple creation of an instance inside the critical section of a lock, and it would throw a null reference exception when I manually dragged the next line to execute. To illustrate:

public class SearchEngineOptimizationParser
{
    protected static ConcurrentDictionary<string, SearchEngineOptimizationInfo> _referralInformation = null;
    protected static DateTime _lastRecordingDate;
    protected static object _lockRecordingObject = new object();

    protected static Dictionary<string, string> _searchProviderLookups = null;

    static SearchEngineOptimizationParser()
    {
        _referralInformation = new ConcurrentDictionary<string, SearchEngineOptimizationInfo>();
        _lastRecordingDate = DateTime.Now;

        _searchProviderLookups = new Dictionary<string, string>();
        _searchProviderLookups.Add("google.com", "q");
        _searchProviderLookups.Add("yahoo.com", "p");
        _searchProviderLookups.Add("bing.com", "q");
    }

    public SearchEngineOptimizationParser()
    {

    }

    public virtual void ParseReferrer(Uri requestUrl, NameValueCollection serverVariables, ISession session)
    {
        string corePath = requestUrl.PathAndQuery.SmartSplit('?')[0].ToLower();

        string referrer = serverVariables["HTTP_REFERER"];

        if (!string.IsNullOrWhiteSpace(referrer))
        {
            NameValueCollection queryString = HttpUtility.ParseQueryString(referrer);

            string dictionaryKey = session.AffiliateID + "|" + corePath;

            foreach (var searchProvider in _searchProviderLookups)
            {
                if (referrer.Contains(searchProvider.Key))
                {
                    if (queryString[searchProvider.Value] != null)
                    {
                        string keywords = queryString[searchProvider.Value];

                        SearchEngineOptimizationInfo info = new SearchEngineOptimizationInfo
                        {
                            Count = 1,
                            CorePath = corePath,
                            AffiliateId = session.AffiliateID,
                            Keywords = keywords
                        };

                        _referralInformation.AddOrUpdate(dictionaryKey, info, (key, oldValue) =>
                        {
                            oldValue.Count++;
                            return oldValue;
                        });

                        break;
                    }
                }
            }
        }

        if (DateTime.Now > _lastRecordingDate.AddHours(1))
        {
            lock (_lockRecordingObject)
            {
                if (DateTime.Now > _lastRecordingDate.AddHours(1))
                {
                    SearchEngineKeywordRepository repository = new SearchEngineKeywordRepository();

                    List<KeyValuePair<string, SearchEngineOptimizationInfo>> currentInfo = _referralInformation.ToList();

                    Action logData = () =>
                    {
                        foreach (var item in currentInfo)
                            repository.LogKeyword(item.Value);
                    };

                    Thread logThread = new Thread(new ThreadStart(logData));
                    logThread.Start();

                    _lastRecordingDate = DateTime.Now;
                    _referralInformation.Clear();
                }
            }
        }
    }

EDIT: Updated Real Object

public class SearchEngineKeywordRepository
{
    public virtual void LogKeyword(SearchEngineOptimizationInfo keywordInfo)
    {
        LogSearchEngineKeywords procedure = new LogSearchEngineKeywords();
        procedure.Execute(keywordInfo.CorePath, keywordInfo.AffiliateId, keywordInfo.Keywords, keywordInfo.Count);
    }
}

The general pattern being that I want to do this ‘something’ only every hour (in the context of a website application that gets a lot of traffic). I would breakpoint my first if statement, and then step the next line to execute inside the second if statement. When doing so, the act of initializing the SomeObject instance would cause a null reference exception. It had a completely 100% default constructor – I didn’t even specify one.

However, when I let the code go through naturally, it would execute without problem. For some reason, it seems that when I skipped over the lock call into the critical section to just test run that code, it caused all kinds of errors.

I’m curious to know why that is; I understand the lock keyword is just syntactic sugar for a Monitor.Enter(o) try / finally block, but that seems to be that when invoking the constructor, something else was happening.

Anyone have any ideas?

EDIT: I’ve added the actual code to this. I’m able to reproduce this at will, but I still don’t understand why this is happening. I’ve tried copying this code to another solution and the problem does not seem to occur.

  • 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-23T15:33:39+00:00Added an answer on May 23, 2026 at 3:33 pm

    I’ve tried to reproduce your situation, but as I expected I could not. I’ve tried both the 2.0 and 4.0 runtime, in 32 and 64 bit mode (debugging sometimes behaves differently under x64).

    Is the code shown a simplification? Have you checked all your assumptions? I understand you’re skipping 3 lines of code, both the if statements and the lock? In that case, even setting the lock object to null does not cause the exception you describe.

    (Having _lockRecordingObject set to null causes an ArgumentNullException when leaving the lock scope)

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

Sidebar

Related Questions

I encountered a strange problem today. Whenever i put a breakpoint in one of
Today I encountered something strange: I tried to put a utility method into an
I've encountered a strange issue with Groovy's (1.7.3) XmlUtil.serialize( GPathResult ) method. It throws
I have encountered a very strange issue. Whenever I use the .NET membership provider
I encountered a strange issue with one of my arrays. The array looks like
I've encountered a strange issue using CompiledQuery.Compile . When trying to use a static
i have encountered a very strange issue: i use json.dump to write a file
I've encountered a very strange issue using JMenuBar where navigating between two JMenus results
I had encountered strange problem while construct a unordeed_set<tuple<int,int>> . I had tried VC++8,
I have encountered strange issue with lists in F#. I am using XamlXmlReader 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.