470,594 Members | 1,472 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,594 developers. It's quick & easy.

Static class members disappearing (GC collected?!)

I have this strange problem now twice: I am writing this relatevely large web
site on 2.0 and I made a static class, which I use for url encoding and
deconding (for remapping purposes). This static class needs the session
context to encode a url (because I stored the current language there), so I
made a static field of type HttpContext, which I refresh every reqest by
assigning the current context.

Now, every now and then I get this runtime error that the Session property
is not set (Object reference not set to instance of an object) and it's
completely random.

I suspect that it has something to do with the fact that the class is static
and GC, but I can't really figure this out.

I had the same problem in 1.1 a while ago with the same site when I wanted
to make the SqlConnection a static property and connect on Application_Start
and disconnect on Application_End. Every n-th request the SqlConnection
disappeared.

Any suggestions?

I pretty much would like to know why this things happen.

May 15 '06 #1
6 1755
The reason is that all requests share the same static variable. Imagine
this scenario:

Request 1 arrives and thread 1 is started to handle it
Thread 1 stores it's context in the static variable
Thread 1 starts working
Request 2 arrives and thread 2 is started to handle it
Thread 2 stores it's context in the static variable
Thread 2 starts working
Thread 1 continues working
Thread 2 uses the static varible - this works fine
Thread 1 uses the static variable - and gets the wrong context
Thread 2 finishes
Thread 2 sends a response to the browser
The context of thread 2 is disposed
Thread 1 tries to use the static variable - and dies

Here you see two problems. The static variable contains the context of
the last request to arrive. That means that some threads will get the
wrong context, and if the last request is first to finish, the static
variable will reference a context that is disposed and no longer has a
reference to a session object.

The solution is to use one instance of the variable for each thread.
This can be done in two ways:

1. Don't use a static class. Create one instance of the class for each page.

2. Use the [ThreadStatic] attribute to create an instance of the static
variable for each thread.

One thing you should be aware of if you choose to use a ThreadStatic
variables, is that the static constructor of the class is only run when
the class is loaded, not for every thread. If you have this code:

[ThreadStatic] static int answer = 42;

The code that initializes the variable is actually placed in the static
constructor. That means that the first thread that uses the class will
get the value 42, but for every thread after that, the value will be zero.
Vladislav Kosev wrote:
I have this strange problem now twice: I am writing this relatevely large web
site on 2.0 and I made a static class, which I use for url encoding and
deconding (for remapping purposes). This static class needs the session
context to encode a url (because I stored the current language there), so I
made a static field of type HttpContext, which I refresh every reqest by
assigning the current context.

Now, every now and then I get this runtime error that the Session property
is not set (Object reference not set to instance of an object) and it's
completely random.

I suspect that it has something to do with the fact that the class is static
and GC, but I can't really figure this out.

I had the same problem in 1.1 a while ago with the same site when I wanted
to make the SqlConnection a static property and connect on Application_Start
and disconnect on Application_End. Every n-th request the SqlConnection
disappeared.

Any suggestions?

I pretty much would like to know why this things happen.

May 15 '06 #2
Göran, thank you for the excellent response and explanation! I didn't think
of that a static variable is used in several requests at one time. With this
in mind, it is obviously a suicide to use a static variable, it will most
certainly crash, that explains the randomness of the errors.

On thing that I think is very interesting is that [ThreadStatic] attribute,
I will certainly read everything there is about it.

But is it certain that 1 request is 1 thread and visa versa?

Because if it is not, this will not work as expected. Actually all I need is
request and/or session level storage, but without using the
HttpContext.Current in my Business Logic tier, because that makes it unusable
in every other context.

"Göran Andersson" wrote:
The reason is that all requests share the same static variable. Imagine
this scenario:

Request 1 arrives and thread 1 is started to handle it
Thread 1 stores it's context in the static variable
Thread 1 starts working
Request 2 arrives and thread 2 is started to handle it
Thread 2 stores it's context in the static variable
Thread 2 starts working
Thread 1 continues working
Thread 2 uses the static varible - this works fine
Thread 1 uses the static variable - and gets the wrong context
Thread 2 finishes
Thread 2 sends a response to the browser
The context of thread 2 is disposed
Thread 1 tries to use the static variable - and dies

Here you see two problems. The static variable contains the context of
the last request to arrive. That means that some threads will get the
wrong context, and if the last request is first to finish, the static
variable will reference a context that is disposed and no longer has a
reference to a session object.

The solution is to use one instance of the variable for each thread.
This can be done in two ways:

1. Don't use a static class. Create one instance of the class for each page.

2. Use the [ThreadStatic] attribute to create an instance of the static
variable for each thread.

One thing you should be aware of if you choose to use a ThreadStatic
variables, is that the static constructor of the class is only run when
the class is loaded, not for every thread. If you have this code:

[ThreadStatic] static int answer = 42;

The code that initializes the variable is actually placed in the static
constructor. That means that the first thread that uses the class will
get the value 42, but for every thread after that, the value will be zero.
Vladislav Kosev wrote:
I have this strange problem now twice: I am writing this relatevely large web
site on 2.0 and I made a static class, which I use for url encoding and
deconding (for remapping purposes). This static class needs the session
context to encode a url (because I stored the current language there), so I
made a static field of type HttpContext, which I refresh every reqest by
assigning the current context.

Now, every now and then I get this runtime error that the Session property
is not set (Object reference not set to instance of an object) and it's
completely random.

I suspect that it has something to do with the fact that the class is static
and GC, but I can't really figure this out.

I had the same problem in 1.1 a while ago with the same site when I wanted
to make the SqlConnection a static property and connect on Application_Start
and disconnect on Application_End. Every n-th request the SqlConnection
disappeared.

Any suggestions?

I pretty much would like to know why this things happen.

May 15 '06 #3
Vladislav Kosev wrote:
Göran, thank you for the excellent response and explanation! I didn't think
of that a static variable is used in several requests at one time. With this
in mind, it is obviously a suicide to use a static variable, it will most
certainly crash, that explains the randomness of the errors.

On thing that I think is very interesting is that [ThreadStatic] attribute,
I will certainly read everything there is about it.

But is it certain that 1 request is 1 thread and visa versa?
Yes. Every request is handled in a separate thread.
Because if it is not, this will not work as expected. Actually all I need is
request and/or session level storage, but without using the
HttpContext.Current in my Business Logic tier, because that makes it unusable
in every other context.

"Göran Andersson" wrote:
The reason is that all requests share the same static variable. Imagine
this scenario:

Request 1 arrives and thread 1 is started to handle it
Thread 1 stores it's context in the static variable
Thread 1 starts working
Request 2 arrives and thread 2 is started to handle it
Thread 2 stores it's context in the static variable
Thread 2 starts working
Thread 1 continues working
Thread 2 uses the static varible - this works fine
Thread 1 uses the static variable - and gets the wrong context
Thread 2 finishes
Thread 2 sends a response to the browser
The context of thread 2 is disposed
Thread 1 tries to use the static variable - and dies

Here you see two problems. The static variable contains the context of
the last request to arrive. That means that some threads will get the
wrong context, and if the last request is first to finish, the static
variable will reference a context that is disposed and no longer has a
reference to a session object.

The solution is to use one instance of the variable for each thread.
This can be done in two ways:

1. Don't use a static class. Create one instance of the class for each page.

2. Use the [ThreadStatic] attribute to create an instance of the static
variable for each thread.

One thing you should be aware of if you choose to use a ThreadStatic
variables, is that the static constructor of the class is only run when
the class is loaded, not for every thread. If you have this code:

[ThreadStatic] static int answer = 42;

The code that initializes the variable is actually placed in the static
constructor. That means that the first thread that uses the class will
get the value 42, but for every thread after that, the value will be zero.
Vladislav Kosev wrote:
I have this strange problem now twice: I am writing this relatevely large web
site on 2.0 and I made a static class, which I use for url encoding and
deconding (for remapping purposes). This static class needs the session
context to encode a url (because I stored the current language there), so I
made a static field of type HttpContext, which I refresh every reqest by
assigning the current context.

Now, every now and then I get this runtime error that the Session property
is not set (Object reference not set to instance of an object) and it's
completely random.

I suspect that it has something to do with the fact that the class is static
and GC, but I can't really figure this out.

I had the same problem in 1.1 a while ago with the same site when I wanted
to make the SqlConnection a static property and connect on Application_Start
and disconnect on Application_End. Every n-th request the SqlConnection
disappeared.

Any suggestions?

I pretty much would like to know why this things happen.

May 15 '06 #4
Is there any chance that a request can be handled by more than one thread? Is
this ThreadStatic reliable?

"Göran Andersson" wrote:
Vladislav Kosev wrote:
Göran, thank you for the excellent response and explanation! I didn't think
of that a static variable is used in several requests at one time. With this
in mind, it is obviously a suicide to use a static variable, it will most
certainly crash, that explains the randomness of the errors.

On thing that I think is very interesting is that [ThreadStatic] attribute,
I will certainly read everything there is about it.

But is it certain that 1 request is 1 thread and visa versa?


Yes. Every request is handled in a separate thread.
Because if it is not, this will not work as expected. Actually all I need is
request and/or session level storage, but without using the
HttpContext.Current in my Business Logic tier, because that makes it unusable
in every other context.

"Göran Andersson" wrote:
The reason is that all requests share the same static variable. Imagine
this scenario:

Request 1 arrives and thread 1 is started to handle it
Thread 1 stores it's context in the static variable
Thread 1 starts working
Request 2 arrives and thread 2 is started to handle it
Thread 2 stores it's context in the static variable
Thread 2 starts working
Thread 1 continues working
Thread 2 uses the static varible - this works fine
Thread 1 uses the static variable - and gets the wrong context
Thread 2 finishes
Thread 2 sends a response to the browser
The context of thread 2 is disposed
Thread 1 tries to use the static variable - and dies

Here you see two problems. The static variable contains the context of
the last request to arrive. That means that some threads will get the
wrong context, and if the last request is first to finish, the static
variable will reference a context that is disposed and no longer has a
reference to a session object.

The solution is to use one instance of the variable for each thread.
This can be done in two ways:

1. Don't use a static class. Create one instance of the class for each page.

2. Use the [ThreadStatic] attribute to create an instance of the static
variable for each thread.

One thing you should be aware of if you choose to use a ThreadStatic
variables, is that the static constructor of the class is only run when
the class is loaded, not for every thread. If you have this code:

[ThreadStatic] static int answer = 42;

The code that initializes the variable is actually placed in the static
constructor. That means that the first thread that uses the class will
get the value 42, but for every thread after that, the value will be zero.
Vladislav Kosev wrote:
I have this strange problem now twice: I am writing this relatevely large web
site on 2.0 and I made a static class, which I use for url encoding and
deconding (for remapping purposes). This static class needs the session
context to encode a url (because I stored the current language there), so I
made a static field of type HttpContext, which I refresh every reqest by
assigning the current context.

Now, every now and then I get this runtime error that the Session property
is not set (Object reference not set to instance of an object) and it's
completely random.

I suspect that it has something to do with the fact that the class is static
and GC, but I can't really figure this out.

I had the same problem in 1.1 a while ago with the same site when I wanted
to make the SqlConnection a static property and connect on Application_Start
and disconnect on Application_End. Every n-th request the SqlConnection
disappeared.

Any suggestions?

I pretty much would like to know why this things happen.

May 15 '06 #5
IIS uses one thread to handle a request. If it started more than one
thread to handle a request, that would mean that there would be more
than one response, and that of course wouldn't work.

You can start new threads from your code, but then you have to take
responsibility for those.

Vladislav Kosev wrote:
Is there any chance that a request can be handled by more than one thread? Is
this ThreadStatic reliable?

"Göran Andersson" wrote:
Vladislav Kosev wrote:
Göran, thank you for the excellent response and explanation! I didn't think
of that a static variable is used in several requests at one time. With this
in mind, it is obviously a suicide to use a static variable, it will most
certainly crash, that explains the randomness of the errors.

On thing that I think is very interesting is that [ThreadStatic] attribute,
I will certainly read everything there is about it.

But is it certain that 1 request is 1 thread and visa versa?

Yes. Every request is handled in a separate thread.
Because if it is not, this will not work as expected. Actually all I need is
request and/or session level storage, but without using the
HttpContext.Current in my Business Logic tier, because that makes it unusable
in every other context.

"Göran Andersson" wrote:

The reason is that all requests share the same static variable. Imagine
this scenario:

Request 1 arrives and thread 1 is started to handle it
Thread 1 stores it's context in the static variable
Thread 1 starts working
Request 2 arrives and thread 2 is started to handle it
Thread 2 stores it's context in the static variable
Thread 2 starts working
Thread 1 continues working
Thread 2 uses the static varible - this works fine
Thread 1 uses the static variable - and gets the wrong context
Thread 2 finishes
Thread 2 sends a response to the browser
The context of thread 2 is disposed
Thread 1 tries to use the static variable - and dies

Here you see two problems. The static variable contains the context of
the last request to arrive. That means that some threads will get the
wrong context, and if the last request is first to finish, the static
variable will reference a context that is disposed and no longer has a
reference to a session object.

The solution is to use one instance of the variable for each thread.
This can be done in two ways:

1. Don't use a static class. Create one instance of the class for each page.

2. Use the [ThreadStatic] attribute to create an instance of the static
variable for each thread.

One thing you should be aware of if you choose to use a ThreadStatic
variables, is that the static constructor of the class is only run when
the class is loaded, not for every thread. If you have this code:

[ThreadStatic] static int answer = 42;

The code that initializes the variable is actually placed in the static
constructor. That means that the first thread that uses the class will
get the value 42, but for every thread after that, the value will be zero.
Vladislav Kosev wrote:
> I have this strange problem now twice: I am writing this relatevely large web
> site on 2.0 and I made a static class, which I use for url encoding and
> deconding (for remapping purposes). This static class needs the session
> context to encode a url (because I stored the current language there), so I
> made a static field of type HttpContext, which I refresh every reqest by
> assigning the current context.
>
> Now, every now and then I get this runtime error that the Session property
> is not set (Object reference not set to instance of an object) and it's
> completely random.
>
> I suspect that it has something to do with the fact that the class is static
> and GC, but I can't really figure this out.
>
> I had the same problem in 1.1 a while ago with the same site when I wanted
> to make the SqlConnection a static property and connect on Application_Start
> and disconnect on Application_End. Every n-th request the SqlConnection
> disappeared.
>
> Any suggestions?
>
> I pretty much would like to know why this things happen.
>

May 15 '06 #6
Thanks a lot!

"Göran Andersson" wrote:
IIS uses one thread to handle a request. If it started more than one
thread to handle a request, that would mean that there would be more
than one response, and that of course wouldn't work.

You can start new threads from your code, but then you have to take
responsibility for those.

Vladislav Kosev wrote:
Is there any chance that a request can be handled by more than one thread? Is
this ThreadStatic reliable?

"Göran Andersson" wrote:
Vladislav Kosev wrote:
Göran, thank you for the excellent response and explanation! I didn't think
of that a static variable is used in several requests at one time. With this
in mind, it is obviously a suicide to use a static variable, it will most
certainly crash, that explains the randomness of the errors.

On thing that I think is very interesting is that [ThreadStatic] attribute,
I will certainly read everything there is about it.

But is it certain that 1 request is 1 thread and visa versa?
Yes. Every request is handled in a separate thread.

Because if it is not, this will not work as expected. Actually all I need is
request and/or session level storage, but without using the
HttpContext.Current in my Business Logic tier, because that makes it unusable
in every other context.

"Göran Andersson" wrote:

> The reason is that all requests share the same static variable. Imagine
> this scenario:
>
> Request 1 arrives and thread 1 is started to handle it
> Thread 1 stores it's context in the static variable
> Thread 1 starts working
> Request 2 arrives and thread 2 is started to handle it
> Thread 2 stores it's context in the static variable
> Thread 2 starts working
> Thread 1 continues working
> Thread 2 uses the static varible - this works fine
> Thread 1 uses the static variable - and gets the wrong context
> Thread 2 finishes
> Thread 2 sends a response to the browser
> The context of thread 2 is disposed
> Thread 1 tries to use the static variable - and dies
>
> Here you see two problems. The static variable contains the context of
> the last request to arrive. That means that some threads will get the
> wrong context, and if the last request is first to finish, the static
> variable will reference a context that is disposed and no longer has a
> reference to a session object.
>
> The solution is to use one instance of the variable for each thread.
> This can be done in two ways:
>
> 1. Don't use a static class. Create one instance of the class for each page.
>
> 2. Use the [ThreadStatic] attribute to create an instance of the static
> variable for each thread.
>
> One thing you should be aware of if you choose to use a ThreadStatic
> variables, is that the static constructor of the class is only run when
> the class is loaded, not for every thread. If you have this code:
>
> [ThreadStatic] static int answer = 42;
>
> The code that initializes the variable is actually placed in the static
> constructor. That means that the first thread that uses the class will
> get the value 42, but for every thread after that, the value will be zero.
>
>
> Vladislav Kosev wrote:
>> I have this strange problem now twice: I am writing this relatevely large web
>> site on 2.0 and I made a static class, which I use for url encoding and
>> deconding (for remapping purposes). This static class needs the session
>> context to encode a url (because I stored the current language there), so I
>> made a static field of type HttpContext, which I refresh every reqest by
>> assigning the current context.
>>
>> Now, every now and then I get this runtime error that the Session property
>> is not set (Object reference not set to instance of an object) and it's
>> completely random.
>>
>> I suspect that it has something to do with the fact that the class is static
>> and GC, but I can't really figure this out.
>>
>> I had the same problem in 1.1 a while ago with the same site when I wanted
>> to make the SqlConnection a static property and connect on Application_Start
>> and disconnect on Application_End. Every n-th request the SqlConnection
>> disappeared.
>>
>> Any suggestions?
>>
>> I pretty much would like to know why this things happen.
>>

May 16 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Samee Zahur | last post: by
9 posts views Thread by Chuck Cobb | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.