473,288 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,288 software developers and data experts.

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 1885
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Lester | last post by:
I have a low level question . . . If I have an assembly with a single class with a static int property, I know the assembly will get loaded the first time the class is referenced. At that...
3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
3
by: Adam Smith | last post by:
Hi. I have a static arraylist in a class which is populated during the class's static constructor. Will this arraylist or the class objects allocated on it ever be garbage collected if I don't...
7
by: Atul Malaviya | last post by:
>From a design/usability perspective. When will one use a singleton pattern and when a class with purely static members? What are the pros and cons? I have inherited a code base which is full...
1
by: J | last post by:
I have an object that I am using in my ASP.net app. I only want one instance of this object available to all of my pages. Here is a sample public class statictes private statictest() {} //...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
9
by: Chuck Cobb | last post by:
I am creating some static classes with static methods and static variables, but I have a question: What is the lifetime of static classes and static variables? Is there any risk that these...
15
by: archana | last post by:
Hi all, can anyone tell me differene between public static and private static method. how they are allocated and access?. thanks in advance.
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.