R# 4.0.7.741 - Expression is always false when null checking against the ASP.NET Cache

So inside a WCF service library I have the following property inside a
class;

static HttpRuntime _httpRuntime;

private static Cache Cache
{
get
{
if (null == _httpRuntime)
lock (_synclock)
if (null == _httpRuntime)
_httpRuntime = new HttpRuntime();

return HttpRuntime.Cache;
}
}

The usual way of using the ASP.NET cache without being inside an ASP.NET
application

Further down inside another method I have the following

string imageCacheKey =
string.Format(CultureInfo.InvariantCulture, "sharpsts_cardImage_", path);
if (null == Cache[imageCacheKey])

The if statement is marked with the warning expression is always false;
however that's not true. If no cache entry exists for that cache key then
the if statement will be true.

If I change the statement to

if (null == Cache.Get(imageCacheKey))

that is treated correctly.

4 comments
Comment actions Permalink

Thank tou for report

--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Barry Dorrans" <news.jetbrains.com@idunno.org> wrote in message
news:fq8brq$lr1$1@is.intellij.net...

So inside a WCF service library I have the following property inside a
class;

>

static HttpRuntime _httpRuntime;

>

private static Cache Cache
{
get
{
if (null == _httpRuntime)
lock (_synclock)
if (null == _httpRuntime)
_httpRuntime = new HttpRuntime();

>

return HttpRuntime.Cache;
}
}

>

The usual way of using the ASP.NET cache without being inside an ASP.NET
application

>

Further down inside another method I have the following

>

string imageCacheKey =
string.Format(CultureInfo.InvariantCulture, "sharpsts_cardImage_",
path);
if (null == Cache[imageCacheKey])

>

The if statement is marked with the warning expression is always false;
however that's not true. If no cache entry exists for that cache key then
the if statement will be true.

>

If I change the statement to

>

if (null == Cache.Get(imageCacheKey))

>

that is treated correctly.



0
Comment actions Permalink

Barry,

There are subtle threading issues with double-checked locking for lazy-init in .NET. The code is broken as read/writes can be reordered by the compiler and CPU.

One solution is to use Joe Duffy's lazy init classes:
http://www.bluebytesoftware.com/blog/2007/06/09/ALazyInitializationPrimitiveForNET.aspx

No need to re-write lots of boilerplate code for static initialization :)

0
Comment actions Permalink


"Oren Novotny" <no_replay@jetbrains.com> wrote in message
news:17459189.1204287929949.JavaMail.itn@is.intellij.net...

Barry,

>

There are subtle threading issues with double-checked locking for
lazy-init in .NET. The code is broken as read/writes can be reordered by
the compiler and CPU.

>

One solution is to use Joe Duffy's lazy init classes:
http://www.bluebytesoftware.com/blog/2007/06/09/ALazyInitializationPrimitiveForNET.aspx

>

No need to re-write lots of boilerplate code for static initialization :)


Or I could just use intialise in a static constructor. I do take your point;
but that doesn't explain the Expression is always null. possible null
reference exception yes; always null, no.

0
Comment actions Permalink

Nope, it's still a bug :)

0

Please sign in to leave a comment.