Una storia di "Memory Leak" (3)

Ecco arrivati al terzo e ultimo atto di "Una storia di "Memory Leak". La risposta di MS sostanzialmente è quella di evitare l'uso di quel tipo di binding su windows forms fx 1.0 e 1.1.

The 1.1 workaround would be to read data from the data source more directly instead of using property binding to do this.

In 2.0, we added a BindingSource class that makes binding a fair amount easier, and this is one of the issues that it fixes: BindingSource is IDisposable, and when it gets disposed it cleans up handlers that it hooks.

La proposta quindi è - nel caso si debbano trattare binding di proprietà di proprietà (nel nostro caso "Gender.Description") - di valorizzare e leggere direttamente tali proprietà, sfruttando gli eventi "PositionChanged" del BindingContext e "Validating" e "Enter" dei controlli come riportato nel codice di esempio legato al caso trattato.

//...

CurrencyManager currencyManager;

public void SetDataSource(IList datasource)
{
 this.datasource = datasource;
 
 Binding bnd_name = new Binding ("Text", datasource, "Name");
 textBox1.DataBindings.Add(bnd_name);
 
 UnhookCurrencyManager();
 
 currencyManager = BindingContext[datasource] as CurrencyManager;
 //bnd_gender = new Binding("Text", datasource, "Gender.Description");
 textBox2.Enter += new EventHandler(textBox2_Enter);
 //textBox2.DataBindings.Add(bnd_gender);
 textBox2.Validating += new CancelEventHandler(textBox2_Validating);
 
 currencyManager.PositionChanged+=new EventHandler(Form2_PositionChanged);
 currencyManager.Position = 0;
 
 UpdateText();
}

private void UnhookCurrencyManager()
{
 if (currencyManager != null)
 {
  currencyManager.PositionChanged -= new EventHandler(Form2_PositionChanged);
 }
}

void textBox2_Validating(object sender, CancelEventArgs e)
{
 Person person = (Person)currencyManager.Current;
 person.Gender.Description = textBox2.Text;
}

void textBox2_Enter(object sender, EventArgs e)
{
 UpdateText();
}

private void UpdateText()
{
 Person person = (Person)BindingContext[datasource].Current;
 textBox2.Text = person.Gender.Description;
}

//...

private void Form2_PositionChanged(object sender, EventArgs e)
{
 label1.Text = string.Format("Position: {0}", currencyManager.Position);
 UpdateText();
}

//...

protected override void Dispose( bool disposing )
{
 if( disposing )
 {
  UnhookCurrencyManager();
  if (components != null)
  {
   components.Dispose();
  }
 }
 base.Dispose( disposing );
}

Ho poi cercato nuovamente in rete per vedere se proprio di questo "problema" nessuno ne ha mai parlato... sembra che il problema fosse trattato da canali MS in "Always Call DataBindings.Clear when Disposing" discusso in questi articoli.

Field Views on Windows Forms 1.0 and 1.1 Data Binding (March 2004)
http://msdn2.microsoft.com/en-gb/library/ms996446.aspx

Memory management and databindings (Wednesday, March 31, 2004)
http://bloggingabout.net/blogs/jschreuder/archive/2004/03/31/704.aspx

oO0( niente hot-fix ma solo un workaround... il caso si può dire chiuso... appuntiamocelo quindi se ci dovesse capitare)

posted @ mercoledì 28 febbraio 2007 00:19

Print
Comments have been closed on this topic.
«aprile»
domlunmarmergiovensab
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011