Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Disposing a stroke's BrushDX properly

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

    Disposing a stroke's BrushDX properly

    What happens to the BrushDX property of a Stroke after the Stroke is garbage collected?
    Is the BrushDX properly disposed?
    Since the Stroke has properties that are IDisposable, why is the Stroke itself not IDisposable?
    Everywhere I look I see code such as the example below that does not bother with proper cleanup and disposal of the BrushDX property of the Stroke class.
    The code below is similar to many of these examples.

    Code:
    // Here's an indicator property stroke initialized to blue.
    public Stroke MyStroke { get; set; } = new Stroke(Brushes.Blue);
    
    public override void OnRenderTargetChanged() {
        if (RenderTarget is null) return;
        MyStroke.RenderTarget = RenderTarget; // automatically creates the BrushDX property of the stroke.
    }
    
    protected override void ToughOnRender(ChartControl chartControl, ChartScale chartScale) {
        // use MyStroke.BrushDX somehow
    }
    Why? Is this sufficient?
    I would have expected the following code also to be necessary.

    Code:
    override OnStateChanged() {
        if (State == State.Terminated) {
            MyStroke?.BrushDX?.Dispose();
            // or even better would be:
           // MyStroke?.Dispose();
        }
    }
    OR, have the NT8 programmers added a Finalize method to the Stroke object that takes care of it when the Stroke is garbage collected?

    OR, is the BrushDX property value recreated EVERY TIME the property is accessed, and are we expected to dispose the value returned every time we access it?

    Also, does the Stroke object dispose old BrushDX objects when its RenderTarget property is updated?
    Last edited by bboyle1234; 02-04-2020, 07:21 PM.

    #2
    I've just run tests and found that a Stroke does replace the BrushDX with a new instance when its RenderTarget property is updated,
    BUT IT DOES NOT DISPOSE THE OLD BRUSHDX INSTANCE!!!!!

    I've also read SharpDX documentation and found that SharpDX Brushes do not have finalizers that dispose them ... they must be explicitly disposed by the using code.
    Last edited by bboyle1234; 02-04-2020, 07:54 PM.

    Comment


      #3
      Here's the SharpDX documentation that says the brushes don't self-dispose when garbage collected:




      Click image for larger version

Name:	com.jpg
Views:	308
Size:	359.6 KB
ID:	1086153
      However, I went ahead and investigated the source code. ComObjects inherit from DisposeBase, which has a DisposeCheck in the finalizer, as shown in the image below.
      In other words, the documentation shown above is wrong, and we can feel free to leave DX Brushes undisposed and garbage collected, so long as NinjaTrader 8 has been deployed using the latest version of the SharpDX software.

      I checked, and NinjaTrader is deployed with SharpDX v2.6.3. The latest version of SharpDX is v4.2.0. Oops. I checked out the SharpDX source code at the v2.6.3 tag and found that the DisposeBase object is exactly the same. I conclude that there is no need to carefully dispose all SharpDX Brush objects if you're happy to let the garbage collector get them.

      Click image for larger version

Name:	finalizer.jpg
Views:	273
Size:	312.9 KB
ID:	1086154

      Comment


        #4
        Hello all,

        When the RenderTarget changes or the Brush changes, the Stroke's SharpDX brush is nulled and is not disposed. Overusing Strokes can carry the same implications as overusing brushes and not disposing them. Strokes also hold a reference locally so they are not constantly recreated.

        OnRender gets called very often which can lead to many SharpDX resources being created. *They should be created in OnRender/OnRenderTargetChnage to ensure valid reference to the active RenderTarget.) Not disposing these can lead to memory issues. We still advise for best practice to dispose and recreate brushes in OnRenderTargetChanged.

        Please let us know if we can be of further assistance.
        JimNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by fx.practic, 10-15-2013, 12:53 AM
        5 responses
        5,404 views
        0 likes
        Last Post Bidder
        by Bidder
         
        Started by Shai Samuel, 07-02-2022, 02:46 PM
        4 responses
        95 views
        0 likes
        Last Post Bidder
        by Bidder
         
        Started by DJ888, Yesterday, 10:57 PM
        0 responses
        7 views
        0 likes
        Last Post DJ888
        by DJ888
         
        Started by MacDad, 02-25-2024, 11:48 PM
        7 responses
        159 views
        0 likes
        Last Post loganjarosz123  
        Started by Belfortbucks, Yesterday, 09:29 PM
        0 responses
        8 views
        0 likes
        Last Post Belfortbucks  
        Working...
        X