Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to release object stored in property?
Message
From
01/05/2009 11:49:33
 
 
To
01/05/2009 06:13:51
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 3.0
OS:
Windows XP SP2
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01397173
Message ID:
01397441
Views:
35
>Hi,
>The book mentioned is out of stock at this moment. However, I have went through some other similar book titles and some online articles. All of them also talk about destructor and IDisposable pattern. Neither of them talk about dangling reference memory clean up. Would it be handled by GC automatically?
>
>For more info, I have object relationship as below
>
>
>public class MyPage : BasePage
>{
>  //-- MyBO will be stored at session
>  private MyBO _myBO = new MyBO();
>
>  private void Page_Load(Object sender, EventArgs e)
>  {
>    //-- this actually via ASP.NET inline markup and will refer to _myBO to show data
>    this.Controls.Add(new MyDataGridView());
>
>    this._myBO.SomeEvent += this.MyBO_SomeEvent;
>  }
>
>  private void MyBO_SomeEvent()
>  {
>    // do something
>  }
>}
>
>public class MyDataGridView : DataGridView
>{
>  public MyDataGridView()
>  {
>    this.Templates.StatusBar = new StatusBarTemplate(this);
>  }
>}
>
>public class StatusBarTemplate : ITemplate
>{
>  private MyDataGridView _grid;
>  private Button _button = new Button();
>
>  public StatusBarTemplate(MyDataGridView grid)
>  {
>    this._grid = grid;
>  }
>
>  public void InstantiateIn(Control container)
>  {
>    this.Controls.Add(this._button);
>    this._button.DataBound += this.Button_DataBound;
>  }
>
>  public void Button_DataBound(Object sender, EventArgs e)
>  {
>    this._button.Enabled = this._grid.SomeMethod();
>  }
>}
>
>
>I use ANT profiler to trace the memory, and I found that some objects is not released (new object created) after every postback.
>
>1. MyBO.SomeEvent
>2. Button
>3. MyDataGridView
>
>Question: How could I free up them upon every postback? In which event/method should I place code to nullify the child object?

There is no requirement to set an object reference to null - it will have NO effect on the .NET garbage collection process.
Also the concept of 'dangling references' is not really valid in .NET - it does not, as older technologies, maintain a reference count to an object.
The only time you should need to worry about this is if your class is using unmanaged code.(or unmanaged components) in which case you should clean up in the destructor. In these cases you can also choose to implement IDisposable and put clean-up code in the Dispose() method - you can then release resources sooner and call SuppressFinalize of the GC so that garbage collection, when it eventually takes place, will be more efficient. But bear in mind that implementing IDisposable isn't of use unless code specifically calls this method (or you use the using{} syntax). The MS 'formal' pattern for implementing IDisposable is something like:
public class ClassThatUsesUnmanagedCode : IDisposable
{
      private bool disposed = false;

      public void Dispose()
     {
          DoTidyUp(true);
          GC.SuppressFinalize(this);
     }

     private void DoTidyUp(bool disposing) 
     {
          if (!this.disposed)
          {
                if (disposing)
                {
                        //Do the tidying up here
                }
                disposed = true;
          }
     }

     //Destructor
    ~ClassThatUsesUnManagedCode()
    {
          DoTidyUp(false);
    }
}
Most of the above is to ensure that the clean-up only gets called once
But, to repeat, none of this is needed unless you are using unmanaged code. Disclaimer: above code is from memory and not checked....
Previous
Reply
Map
View

Click here to load this message in the networking platform