Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Possible misunderstanding in NT DOCS causing a big memory leak if incorrectly used

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Possible misunderstanding in NT DOCS causing a big memory leak if incorrectly used

    After 8.0.x.x release, the NinjaTrader documentation was updated and advising us to use OnRenderTargetChanged() as best place for disposing the resources used in OnRender.

    So, till date we have been used to use such approach:


    Code:
    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.

    Last edited by ttodua; 06-09-2020, 04:48 AM.

    #2
    Hello ttodua,

    Thank you for your post.

    I'm not sure if you've reviewed this page of the help guide, but it covers quite a bit of what you're pointing out here:



    Is there further information you feel should be included here?

    Thanks in advance; I look forward to assisting you further.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    646 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    367 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    108 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    570 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    573 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X