reassigning static variable
-
static object s_obj = new object();
then in some function,
void func()
{
object obj = new object();
s_obj = obj;
}Honestly, I have never seen such a code before. X| When someone does this, what actually happens to the memory, statically allocated to s_obj, initially? It's left hanging on the air without any reference ?
Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy Falcon.
-
static object s_obj = new object();
then in some function,
void func()
{
object obj = new object();
s_obj = obj;
}Honestly, I have never seen such a code before. X| When someone does this, what actually happens to the memory, statically allocated to s_obj, initially? It's left hanging on the air without any reference ?
Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy Falcon.
-
Depending on garbage collection the memory should be released, because the ref counter of the original object is set to zero in the func() assigment.:~
Press F1 for help or google it. Greetings from Germany
-
Thanks, But does this apply even to Static Variables?
Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy Falcon.
Yes; the object ("new object") is not static; the pointer-variable holding that object is. Once another object is assigned to it, the other object is no longer referenced.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
-
Yes; the object ("new object") is not static; the pointer-variable holding that object is. Once another object is assigned to it, the other object is no longer referenced.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
-
Depending on garbage collection the memory should be released, because the ref counter of the original object is set to zero in the func() assigment.:~
Press F1 for help or google it. Greetings from Germany
KarstenK wrote:
the ref counter of the original object is set to zero
If it's .NET, there's no ref counter. Instead, the GC will detect that the object is no longer reachable. That way, circular references won't leak memory. :)
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer