Removing methods calls off the stack in C#

How can I remove all the method calls off the stack of an object in C#?I set the variable equal to null but the object is still executing the code.
For example if I have onc class calling a method of the other and that calling other methods in that class like this:
| |
class A {private B var; void start() { var =new B; var.runFlagtrue; var.run(); } void stop() { var.runFlag =false; var.Dispse(); var =null; } } class B : IDisposable { publicbool runFlag =false; void run() { while(runFlag) { meth1(); meth2(); meth3(); } } void meth1() { doSomething(); } void meth2() { doSomething(); } void meth3() { doSomething(); } void doSomething() { return; } void Dispose() { // clean the resources }}
|
Then after the call in the stop() method of class A, B should be garabage collected no matter where the control of execution is. right or wrong.
Please help!!!!!!!!!!!!!!!!!!!!!!!!
At first, try adding
"volatile" keyword to your flag.
public volatile bool runFlag = false;
This will safely propogate changes in your true/false to all threads.
At second - intance of B will be passed as additional argument to all methods to be accessible as "this" pointer.
Thus - even if you called Dispose() - then B() will ocupy a few memory.
Even more - if all references will be null-ed out - it can take a very long time for GC to free memory.
TAG at 2007-9-8 >

Thanks for your reply TAG.
You are right I would have to make that runFlag volatile.
As for the instance of B (this), is it explicitly passed? because I am not passing it in my method calls anywhere.
I used GC.Collect(); to force the GC to collect all the garabage and it seem to work, but it's still a little mystry.
--hbuchal_dotnet
Yep. For non-static methods - having method on stack will prevent object referenced as "this" to be collected.
Calling GC.Collect() is bad. You are forcing early collection of ALL objects. Take a read this http://blogs.msdn.com/ricom/archive/2004/11/29/271829.aspx
Better try to use IDisposable.Dispose() or some kind of Close() / Terminate() methods to remove/delete/close all objects you need.
Allow GC to do his job well.
TAG at 2007-9-8 >

I think I need to explain something to you. Removing an object off the stack can be done, but you are not "forcing" the GC to pull it off, you are only "asking" it to do it. Now, if the GC determines it is the right time to pull that object off, then it will do your biding, but if it has not, then it won't do it. The GC is the all knowing, omni-present, greater-being. You can only and ask, you cannot command.
Example:
using System;
| | namespace UnManagedResourceObjLib { public class OwnsUnmanagedResources : IDisposable { public OwnsUnmanagedResources() { Console.WriteLine("OwnsUnmanagedResources obj constructed"); } ~OwnsUnmanagedResources() { Console.WriteLine("OwnsUnmanagedResources obj DE-structed (Finalize called)"); CleanUp(); } void CleanUp() { Console.WriteLine("OwnsUnmanagedResources obj cleanup occurred"); } public void Close() { this.Dispose(); } #region IDisposable Members bool _disposed = false; public void Dispose() { if(this._disposed) return; else this._disposed = true; GC.SuppressFinalize(this); Console.WriteLine("OwnsUnmanagedResources obj Disposed"); CleanUp(); } #endregion public void SomeOtherOperation() { if(this._disposed) throw new ObjectDisposedException("OwnsUnmanagedResources"); Console.WriteLine("OwnsUnmanagedResources obj: SomeOtherOperation"); } } } |
| | using System; namespace FunWithGC { class App { static void UsesObj() { #if EVIL UnManagedResourceObjLib.OwnsUnmanagedResources obj = new UnManagedResourceObjLib.OwnsUnmanagedResources(); // obj.Dispose(); // obj.Dispose(); // obj is out of scope obj.SomeOtherOperation(); using(obj) { obj.SomeOtherOperation(); } obj.SomeOtherOperation(); #else using(UnManagedResourceObjLib.OwnsUnmanagedResources obj = new UnManagedResourceObjLib.OwnsUnmanagedResources()) { using(UnManagedResourceObjLib.OwnsUnmanagedResources obj2 = new UnManagedResourceObjLib.OwnsUnmanagedResources()) obj2.SomeOtherOperation(); obj.SomeOtherOperation(); }#endif } static void Main() { Console.WriteLine("Main started"); Console.WriteLine("UsesObj method called"); UsesObj(); Console.WriteLine("UsesObj method returned"); Console.WriteLine("Main terminating"); } } } |
reference the UnManagedResourceObject to the FunWithGC. You will understand how the GC destroys the object and takes it off the stack.