locked
A probably stupid question about thread scope... RRS feed

  • Question

  • I have this code...

    private void butMake_Click(object sender, EventArgs e)
    {
        MyTimer.Start();
    
        Thread t = new Thread(new ThreadStart (Go));
        t.Start();
    }

    ...which is based on some example threading code I found, and which works. But I suddenly saw this and thought: "Hey, but doesn't t go out of scope as soon as I've called .Start?

    Presumably t does not go out of scope but it exists in its own memory space away from the one which created it.

    Formally and practically what happens?


    http://www.ransen.com Cad and Graphics software

    Saturday, July 30, 2016 3:16 PM

Answers

  • Thread is a reference type, so the value of the variable Thread t is only a reference, and the Thread instance itself is stored elsewhere in memory. After the variable goes out of scope, the garbage collector could collect the instance if there were no other references; but the thread is alive (i.e. it was started and not yet terminated) and that counts as a reference.

    Addendum: the garbage collector can collect an instance even before a local variable referring to that instance goes out of scope, if the method isn't going to read the variable after that point. See the GC.KeepAlive method.

    • Edited by ranta Saturday, July 30, 2016 6:03 PM GC.KeepAlive
    • Proposed as answer by Christopher84 Sunday, July 31, 2016 2:37 AM
    • Marked as answer by Owen Ransen Sunday, July 31, 2016 5:42 AM
    Saturday, July 30, 2016 5:58 PM
  • If an object is not more referenced, then it becomes candidate to be deleted. In your program, the ‘Thread’ object it is not referenced explicitly after exiting from the function. However, it is possible that an object is still referenced implicitly by some hidden code. For example, maybe the new Thread constructor adds this to some internal list. Or maybe it is done inside the Start. Later the saved object is available via Thread.CurrentThread property inside the Go function.

    • Marked as answer by Owen Ransen Sunday, July 31, 2016 5:43 AM
    Saturday, July 30, 2016 6:06 PM

All replies

  • Thread is a reference type, so the value of the variable Thread t is only a reference, and the Thread instance itself is stored elsewhere in memory. After the variable goes out of scope, the garbage collector could collect the instance if there were no other references; but the thread is alive (i.e. it was started and not yet terminated) and that counts as a reference.

    Addendum: the garbage collector can collect an instance even before a local variable referring to that instance goes out of scope, if the method isn't going to read the variable after that point. See the GC.KeepAlive method.

    • Edited by ranta Saturday, July 30, 2016 6:03 PM GC.KeepAlive
    • Proposed as answer by Christopher84 Sunday, July 31, 2016 2:37 AM
    • Marked as answer by Owen Ransen Sunday, July 31, 2016 5:42 AM
    Saturday, July 30, 2016 5:58 PM
  • If an object is not more referenced, then it becomes candidate to be deleted. In your program, the ‘Thread’ object it is not referenced explicitly after exiting from the function. However, it is possible that an object is still referenced implicitly by some hidden code. For example, maybe the new Thread constructor adds this to some internal list. Or maybe it is done inside the Start. Later the saved object is available via Thread.CurrentThread property inside the Go function.

    • Marked as answer by Owen Ransen Sunday, July 31, 2016 5:43 AM
    Saturday, July 30, 2016 6:06 PM
  • Another example is the whole WinForms System. Even if your reference to a form goes out of scope, the Windowing System will still maintain a reference to it until you call Close().

    Especially when using Hide(), this can cause reference memory leaks.

    The full terminology for Garbage Collection lockout is:
    "Has a unbroken chain of references to a application root." That rule also resolves issues that might arise from circular references.

    Sunday, July 31, 2016 2:44 AM
  • Thanks to all of you for the great replies...

    http://www.ransen.com Cad and Graphics software

    Sunday, July 31, 2016 5:44 AM