Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a static variable in a library (DLL) shared by all processes referencing that library?

Tags:

I know that a static variable used in a web application is shared for all users across the web application. If I have a library (DLL) that uses some static private variable, do all applications using that library share the value of that variable?

For example, say I have the following code in my DLL:

private static bool isConnected = false;  public static void Connect() {     // TODO: Connect.     isConnected = true; }  public static void Disconnect() {     // TODO: Disconnect.     isConnected = false; } 

Then in Application A, I call myDLL.Connect() which sets the value of isConnected to True. Then I have some Application B that does the same thing. If Application A later calls myDLL.Disconnect(), does Application B see isConnected as False because the two applications share the same DLL file with a static variable? The DLL file would, in this case, be literally the same file in the same file path.

(I previously asked a somewhat similar question about web applications here. It is not related.)

like image 763
Devin Burke Avatar asked Jul 25 '11 14:07

Devin Burke


People also ask

Are static variables shared?

Class Variables and MethodsA static variable is shared by all instances of a class. Only one variable created for the class.

Are static variables shared by all objects?

static attributes exist independently of any instances of a class and may be accessed even when no instances of the class have been created. You can compare a static variable with a shared variable. A static variable is shared by all the objects of a class.

Can program access the global variables value of DLL or dynamic library?

In reality, "yes" the processes do have full "access" to the globals, at the very least through the funtion calls to the library.

Where do static variables get stored?

The static variables are stored in the data segment of the memory. The data segment is a part of the virtual address space of a program. All the static variables that do not have an explicit initialization or are initialized to zero are stored in the uninitialized data segment( also known as the BSS segment).


1 Answers

No they won't. They are loaded in separate AppDomains and cannot see each other's instances.

Even if they refer to same physical file, each application gets its own private instance of the assembly.

like image 55
Mrchief Avatar answered Oct 05 '22 01:10

Mrchief