﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>User24 commented on Find the bug: Why do I get a Null Reference Exception?</title><description>If i would have spend less time in looking how to get rhino mocks installed,  i would have more time for things like this (finding NULL References).

Kind Regards &amp;&amp; sorry for wrong Post ;)

</description><link>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment32</link><guid>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment32</guid><pubDate>Thu, 19 May 2011 10:45:35 GMT</pubDate></item><item><title>Ayende Rahien commented on Find the bug: Why do I get a Null Reference Exception?</title><description>User24,
And this has what to do with this post?</description><link>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment31</link><guid>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment31</guid><pubDate>Thu, 19 May 2011 09:25:31 GMT</pubDate></item><item><title>User24 commented on Find the bug: Why do I get a Null Reference Exception?</title><description>How to install rhino mocks? the usability of this page is very bad. where can i get binaries. i dont want to install git or something like that. 
this is to freaky. it should be easier to use at all.</description><link>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment30</link><guid>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment30</guid><pubDate>Thu, 19 May 2011 09:20:57 GMT</pubDate></item><item><title>Mark Knell commented on Find the bug: Why do I get a Null Reference Exception?</title><description>lock() is not safe against reentrant code on the same thread. So maybe the implementation of properties.Clone() recurses to the block of code in question (and does not infinitely recurse, since we didn't get a stack overflow; suppose it has some sort of recursion limit).  It will re-enter the lock, Clone() once more, null out the parentObject (I'm assuming it's not a local variable on the stack, since you're locking on it), then unwind to find parentObject null.  Boom.
</description><link>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment29</link><guid>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment29</guid><pubDate>Tue, 03 May 2011 19:55:55 GMT</pubDate></item><item><title>Peter Gfader commented on Find the bug: Why do I get a Null Reference Exception?</title><description>  
properties is null
  
or
  
parentKey is null
  
  
This line is wierd
  
properties = properties.Clone();
  
</description><link>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment28</link><guid>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment28</guid><pubDate>Sat, 23 Apr 2011 07:10:27 GMT</pubDate></item><item><title>Fabian Schmied commented on Find the bug: Why do I get a Null Reference Exception?</title><description>If some other piece of code sets parentObject to a different (non-null) value, then the parentObject you lock on might not be the same parentObject you use a few lines down. This might cause a NullReferenceException (and other strange behavior).
  
  
This is far-fetched, since the snippet doesn't show anyone setting parentObject, but still... Locking an object that can be replaced (by null or whatever) doesn't seem like a good idea to me.
</description><link>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment27</link><guid>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment27</guid><pubDate>Thu, 21 Apr 2011 14:01:06 GMT</pubDate></item><item><title>Abhi commented on Find the bug: Why do I get a Null Reference Exception?</title><description>I think "properties" is not initialized, so when calling properties.clone() , you get a NRE ..?
</description><link>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment26</link><guid>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment26</guid><pubDate>Thu, 21 Apr 2011 04:34:24 GMT</pubDate></item><item><title>Basarat commented on Find the bug: Why do I get a Null Reference Exception?</title><description>The object used for a lock is being set to null inside the lock. 
</description><link>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment25</link><guid>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment25</guid><pubDate>Thu, 21 Apr 2011 03:55:15 GMT</pubDate></item><item><title>Duarte Nunes commented on Find the bug: Why do I get a Null Reference Exception?</title><description>@Bek Yep. My initial mix up stemmed from a bit too much java programming, where there's no equivalent to ArgumentNullException: when validating parameters, you throw a NullPointerException.
</description><link>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment24</link><guid>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment24</guid><pubDate>Wed, 20 Apr 2011 15:40:20 GMT</pubDate></item><item><title>Duarte Nunes commented on Find the bug: Why do I get a Null Reference Exception?</title><description>That's not it. There's a dataflow dependence so the compiler would never make that optimization. Also, on the CLR, all stores have release semantics, meaning there's an implicit barrier that does not allow stores to be reordered with other stores.
</description><link>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment23</link><guid>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment23</guid><pubDate>Wed, 20 Apr 2011 15:37:26 GMT</pubDate></item><item><title>Alex Espinoza commented on Find the bug: Why do I get a Null Reference Exception?</title><description>The compiler is optimizing the output with reordering:
  
  
Compiler Reordering: 
  
     parentObject = null;
  
     parentObject[parentKey] = this;
  
  
This can be prevented if a barrier is used and/or making the object volatile to avoid reodering.
  
</description><link>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment22</link><guid>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment22</guid><pubDate>Wed, 20 Apr 2011 15:29:45 GMT</pubDate></item><item><title>Bek commented on Find the bug: Why do I get a Null Reference Exception?</title><description>One more thing: generally the framework doesn't allow NRE to bubble up. it's usually something more meaningful. It has to be in his code. 
</description><link>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment21</link><guid>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment21</guid><pubDate>Wed, 20 Apr 2011 15:27:01 GMT</pubDate></item><item><title>Duarte Nunes commented on Find the bug: Why do I get a Null Reference Exception?</title><description>Well, since all accesses to "parentObject" are safe, and Monitor.Enter throws ArgumentNullException, then I guess "properties" must be null.
</description><link>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment20</link><guid>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment20</guid><pubDate>Wed, 20 Apr 2011 15:26:54 GMT</pubDate></item><item><title>Bek commented on Find the bug: Why do I get a Null Reference Exception?</title><description>even in 4.0 Enter throws an ArgumentNullException. if obj is null.   
</description><link>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment19</link><guid>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment19</guid><pubDate>Wed, 20 Apr 2011 15:24:07 GMT</pubDate></item><item><title>Duarte Nunes commented on Find the bug: Why do I get a Null Reference Exception?</title><description>Oh yeah, you're right. Don't know about the NullReferenceException then.
</description><link>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment18</link><guid>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment18</guid><pubDate>Wed, 20 Apr 2011 15:23:09 GMT</pubDate></item><item><title>Bek commented on Find the bug: Why do I get a Null Reference Exception?</title><description>Monitor.Enter will throw an ArgumentNullException...Not sure about NullReferenceException. 
</description><link>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment17</link><guid>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment17</guid><pubDate>Wed, 20 Apr 2011 15:17:30 GMT</pubDate></item><item><title>Duarte Nunes commented on Find the bug: Why do I get a Null Reference Exception?</title><description>Actually, on .NET 4.0, lock (obj) translates into try { Monitor.Enter(local = obj, ref lockTaken); ... } finally { if (lockTaken) Monitor.Exit(local); }. This means that the problem is not the Exit method; the Enter method is the one throwing NRE.
</description><link>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment16</link><guid>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment16</guid><pubDate>Wed, 20 Apr 2011 15:17:12 GMT</pubDate></item><item><title>Oleksii commented on Find the bug: Why do I get a Null Reference Exception?</title><description>The LOCK statement is converted into two methods:
  
  
==============
  
Monitor.Enter(object o)
  
//code
  
Monitor.Exit(object o)
  
==============
  
  
In the Exit method the locking object has been set to null, and as described in the documentation of the method an ArgumentNullException would be thrown.
</description><link>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment15</link><guid>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment15</guid><pubDate>Wed, 20 Apr 2011 15:09:14 GMT</pubDate></item><item><title>Adam commented on Find the bug: Why do I get a Null Reference Exception?</title><description>Its no NRE, but other ArgumentNullException would be if a second entrant attempted to lock ParentObject after it was set to null.
  
  
2nd Entrant -&gt; parentObject != null, continue
  
1st entrant -&gt; parentobject = null
  
2nd Entrant -&gt; lock(parentObject), throws
  
  
Setting the lock to null seems like a bad idea.
</description><link>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment14</link><guid>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment14</guid><pubDate>Wed, 20 Apr 2011 14:05:16 GMT</pubDate></item><item><title>Linkgoron commented on Find the bug: Why do I get a Null Reference Exception?</title><description>I've run the code and Monitor.Exit does not throw an exception. I'm guessing that lock creates a temp reference object.
  
  
I wish we had a bit more context to this (i/e scope of variables etc.)
</description><link>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment13</link><guid>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment13</guid><pubDate>Wed, 20 Apr 2011 12:06:25 GMT</pubDate></item><item><title>Adam commented on Find the bug: Why do I get a Null Reference Exception?</title><description>You are setting the lock object to null.
  
When the lock context ends, Monitor.Exit(parentObject) will be called and will throw.
  
But I thought it threw ArgumentNullException as well
</description><link>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment12</link><guid>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment12</guid><pubDate>Wed, 20 Apr 2011 10:33:49 GMT</pubDate></item><item><title>hangy commented on Find the bug: Why do I get a Null Reference Exception?</title><description>Okay, I was wrong. The (now removed) parentObjectCopy being null would have caused an ArgumentNullException, as Frederik said.
  
  
Iain: parentKey being null should cause an ArgumentNullException, too.
  
  
In that case, setting properties (let's assume it's a instance variable) to properties.Clone() does seem suspicious ...
</description><link>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment11</link><guid>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment11</guid><pubDate>Wed, 20 Apr 2011 09:35:45 GMT</pubDate></item><item><title>Patrick Huizinga commented on Find the bug: Why do I get a Null Reference Exception?</title><description>As mjkirkham says, there's a race condition.
  
  
When a context switch happens right after the first if statement, but before the lock, another thread could execute "parentObject = null;". When the 'first' thread then resumes control, it will try to lock on a parentObject which is now null.
</description><link>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment10</link><guid>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment10</guid><pubDate>Wed, 20 Apr 2011 09:35:12 GMT</pubDate></item><item><title>mjkirkham commented on Find the bug: Why do I get a Null Reference Exception?</title><description>Looks like you have a race condition between checking the parentObject for null and then attempting to lock it.
</description><link>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment9</link><guid>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment9</guid><pubDate>Wed, 20 Apr 2011 09:29:07 GMT</pubDate></item><item><title>Iulian Margarintescu commented on Find the bug: Why do I get a Null Reference Exception?</title><description>It depends on what kind of variables parentObject &amp; parentObjectCopy are. If parentObjectCopy is not shared between threads ( private static ) then the lock is kind of useless since other threads will not block on it and can set partenObject to null while other threads try to call the indexer.
</description><link>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment8</link><guid>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment8</guid><pubDate>Wed, 20 Apr 2011 09:23:07 GMT</pubDate></item><item><title>Ayende Rahien commented on Find the bug: Why do I get a Null Reference Exception?</title><description>Fredrik,
  
You are correct, I published part of the solution to this, opps. Fixed now
</description><link>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment7</link><guid>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment7</guid><pubDate>Wed, 20 Apr 2011 09:21:26 GMT</pubDate></item><item><title>Alex Reitbort commented on Find the bug: Why do I get a Null Reference Exception?</title><description>Why would you do this:
  
  
properties = properties.Clone();
  
  
What purpose does it serve?
</description><link>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment6</link><guid>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment6</guid><pubDate>Wed, 20 Apr 2011 09:18:52 GMT</pubDate></item><item><title>Iain commented on Find the bug: Why do I get a Null Reference Exception?</title><description>either:
  
properties is null
  
or
  
parentKey is null
</description><link>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment5</link><guid>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment5</guid><pubDate>Wed, 20 Apr 2011 09:14:35 GMT</pubDate></item><item><title>hangy commented on Find the bug: Why do I get a Null Reference Exception?</title><description>.NET may fail when trying to `lock (parentObjectCopy)` and that is null. You probably wanted to lock on parentObject instead.
</description><link>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment4</link><guid>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment4</guid><pubDate>Wed, 20 Apr 2011 09:13:48 GMT</pubDate></item><item><title>Fredrik M&amp;#246;rk commented on Find the bug: Why do I get a Null Reference Exception?</title><description>If parentObjectCopy is null you should get an ArgumentNullException, so my vote goes to properties being null.
</description><link>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment3</link><guid>http://ayende.com/4817/find-the-bug-why-do-i-get-a-null-reference-exception#comment3</guid><pubDate>Wed, 20 Apr 2011 09:11:23 GMT</pubDate></item></channel></rss>