locked
Variable and Scope RRS feed

  • Question

  • Alright say I have a global

    List<MyObject> gObj = new List<MyObject>();

    and then is a local function I do something like so

    MyObject obj = new MyObject() {
    
    // Set variables in MyObject
    
    };
    
    gObj.Add(obj);
    

    When I leave this local function what will happen to the obj object?

    Wednesday, September 30, 2015 1:16 PM

Answers

  • obj is a local variable on the stack so it goes away and memory for it is deallocated just like every other stack-based language.  That's how runtime stacks work. 

    The more likely question you had was whether the new'ed up object is still around and the answer to that is yes.  Since MyObject is most likely a class it gets allocated in the heap.  At some point in the future the GC will run and, if the object is no longer referenced, free the memory for the object.  It is important to separate the concept of a variable from the actual object. They are completely separate things. 

    If we limit the discuss to just reference types then a variable is simply a reference/pointer to the actual object sitting in the heap somewhere.  The lifetime of a variable is its scope just like most languages work.  The lifetime of the object it references is determined by the GC.  You can have any # of variables reference the same object.

    As long as at least one variable references an object (directly or indirectly) then the underlying object will not be freed.  Since you're passing the object (represented by your variable) to a global object that won't go away then the GC considers the object referenced and won't free it.  However your local variable will long since have gone away.

    Michael Taylor
    http://blogs.msmvps.com/p3net

    • Proposed as answer by Magnus (MM8)MVP Wednesday, September 30, 2015 3:58 PM
    • Marked as answer by Kristin Xie Thursday, October 8, 2015 3:43 AM
    Wednesday, September 30, 2015 2:11 PM
  • This:

    List<MyObject> gObj = new List<MyObject>();

    Is not what I'd call a global.

    It has scope of whatever class or method it's defined within.

    So that "local function" is presumably a method in the same class and nothing happens until the owning class is disposed.

    For example:

        public class Class1
        {
            List<object> gObj = new List<object>();
            private void SomeMethod()
            {
                gObj.Add(new object());
            }
        }

    I new up an new instance of Class1 and it has a gObj with no objects in it.

    I call SomeMethod on that and it adds a new object.

    Call it again, it adds another one.

    They remain until that instance of class1 goes out of scope or is disposed.


    Hope that helps.

    Technet articles: All my Technet Articles

    • Proposed as answer by DotNet Wang Thursday, October 1, 2015 5:09 AM
    • Marked as answer by Kristin Xie Thursday, October 8, 2015 3:43 AM
    Wednesday, September 30, 2015 4:11 PM

All replies

  • Since your List<T> is global, then all methods in the class can see the object. So what you are doing is normal.
    Wednesday, September 30, 2015 1:39 PM
  • Since your List<T> is global, then all methods in the class can see the object. So what you are doing is normal.
    Thanks for the reply. I was wondering once obj (local to the function) goes out of scope if it will still be using allocated memory or will/should it be cleaned up or disposed of? Will it linger?
    Wednesday, September 30, 2015 1:54 PM
  • obj is a local variable on the stack so it goes away and memory for it is deallocated just like every other stack-based language.  That's how runtime stacks work. 

    The more likely question you had was whether the new'ed up object is still around and the answer to that is yes.  Since MyObject is most likely a class it gets allocated in the heap.  At some point in the future the GC will run and, if the object is no longer referenced, free the memory for the object.  It is important to separate the concept of a variable from the actual object. They are completely separate things. 

    If we limit the discuss to just reference types then a variable is simply a reference/pointer to the actual object sitting in the heap somewhere.  The lifetime of a variable is its scope just like most languages work.  The lifetime of the object it references is determined by the GC.  You can have any # of variables reference the same object.

    As long as at least one variable references an object (directly or indirectly) then the underlying object will not be freed.  Since you're passing the object (represented by your variable) to a global object that won't go away then the GC considers the object referenced and won't free it.  However your local variable will long since have gone away.

    Michael Taylor
    http://blogs.msmvps.com/p3net

    • Proposed as answer by Magnus (MM8)MVP Wednesday, September 30, 2015 3:58 PM
    • Marked as answer by Kristin Xie Thursday, October 8, 2015 3:43 AM
    Wednesday, September 30, 2015 2:11 PM
  • It will linger, as the gObj now has a reference to it. The garbage collector can only clear up an object once all references to it have gone.
    Wednesday, September 30, 2015 2:12 PM
  • This:

    List<MyObject> gObj = new List<MyObject>();

    Is not what I'd call a global.

    It has scope of whatever class or method it's defined within.

    So that "local function" is presumably a method in the same class and nothing happens until the owning class is disposed.

    For example:

        public class Class1
        {
            List<object> gObj = new List<object>();
            private void SomeMethod()
            {
                gObj.Add(new object());
            }
        }

    I new up an new instance of Class1 and it has a gObj with no objects in it.

    I call SomeMethod on that and it adds a new object.

    Call it again, it adds another one.

    They remain until that instance of class1 goes out of scope or is disposed.


    Hope that helps.

    Technet articles: All my Technet Articles

    • Proposed as answer by DotNet Wang Thursday, October 1, 2015 5:09 AM
    • Marked as answer by Kristin Xie Thursday, October 8, 2015 3:43 AM
    Wednesday, September 30, 2015 4:11 PM