So, till date we have been used to use such approach:
SharpDX.Direct2D1.Brush dxBrush;
public override void OnRenderTargetChanged()
{
try
{
base.OnRenderTargetChanged();
if (dxBrush != null) dxBrush.Dispose();
// Re-create RenderTarget is recreated
if (RenderTarget != null)
{
}
}
catch (Exception ex)
{
m(ex);
}
}
protected override void OnRender (ChartControl chartControl, ChartScale chartScale)
{
base.OnRender(..);
...
...
...
dxBrush = myLevel.PlotBrushes(chartIndex).ToDxBrush();
....
...
...
}
because I was thinking that the Disposable objects should be disposed in OnRenderTargetChanged (ORTC), thus allowing the `private ... ` scope variables (i.e. `dxBrush` as example) to freely used in any place in OnRender to re-create the brushes and then finally dispose safely in ORTC.
However, after digging a bug in one indicator, a light came to my mind and have said - "ouch, why i havent thought about it..." and found out very stupid mistake I've been following.
The Disposable resources should not be created in OnRender if they are disposed in ORTC, because re-creationg (or re-writing) the Disposable objects with same variable name, make them to loose reference to the object (because of overwriting), causing huge memory leak (because on each OnRender the "overwriten" and lost Disposable objects will increase drastically on each OnRender).
So, in DOCS, please specifically mention this fact with RED cautious words.
Only those Disposable objects that are created once per indicator lifetime (and not dynamicall need to re-create them in OnRender), only they are safe to be created & destroyed in ORTC.
If Disposable object needs to be re-created in OnRender,they should have nothing to do with ORTC, and MUST be disposed in the same OnRender scope, not to allow possible re-assigning the new Disposable object to that same variable (also, this needs to be also stated in DOCS, to warn users not to "overwrite" disposable objects within same variables (defined in outer scope), and even if they will do, they should at first dispose the variable and after that it's safe to re-assign new Dispisable object to that variable.... however, as best practice, it may be better to create disposable variables within local scope always to avoid it's usage/rewriting/etc in other scopes).
thanks.

Comment