Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

What should I dispose?

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

    What should I dispose?

    I noticed in the stock NT8 indicators that DXBrushes are disposed within OnRender(). So I've been doing that in my custom indicators. But is there a way for me to know what I should be disposing without comparing my work to the stock indicators? Is there a list somewhere?

    I'm one of those brute force type guys who can make happen what I need to make happen but does not have a programming background.

    Thanks for your help. I think NT8 looks great and I'm close to being able to take it for a spin in the live market.

    #2
    Hello TheBarChartist,

    Thank you for writing in. You should dispose of items when they are no longer needed. If a class implements the IDisposable interface, you should always call the Dispose() method once you have finished using this resource. To ensure that the Dispose() method will be called even if an exception is thrown, you can wrap it in a using statement:
    Code:
    using(var redBrush = new SolidBrush(Color.Red)
    {
        g.DrawString(valueText, Font, redBrush);
    }
    Please let me know if I may be of further assistance.
    Michael M.NinjaTrader Quality Assurance

    Comment


      #3
      Since I'll often want to create a resource such as a brush and use it multiple times and only dispose of it when closing the indicator, where is the proper place to create and dispose of a brush?

      Code:
      SharpDX.Direct2D1.Brush brushDown = BarDownBrush.ToDxBrush(RenderTarget);
      Should this be done in OnStateChange()?

      State == State.SetDefaults
      and
      State == State.Terminated

      Comment


        #4
        Hello overflowing,

        You would create the brush when needed. And you could remove it it the Terminated State if you prefer. Essentially, add it when you need it, remove it when you no longer need it.

        Comment


          #5
          I notice in the VolumeZones sample indicator the DX brush is instantiated and disposed of inside OnRender(), e.g.:

          Code:
          protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
          {
            SharpDX.Direct2D1.Brush brushDown = BarDownBrush.ToDxBrush(RenderTarget);
            ...
            brushDown.Dispose();
          }
          Surely this isn't efficient if OnRender is called many times? Just seems like it makes more sense to construct and dispose of the brushes outside of OnRender(). Unless there is some reason not to?

          Comment


            #6
            Or are brushes so lightweight that it doesn't matter?

            Comment


              #7
              The reading I've done seems to lean in the direction of not caching drawing objects basically because they are cheap to create and there are limited number of GDI resources available. Seems like the best practice may be to either use system pens and brushes (e.g. Brushes.Red) or use a series of using statements inside the OnRender() method.

              Only if you run into a performance issue (generally unlikely) should you consider refactoring to caching drawing objects.


              See:
              Is it better approach to use a new Brush in Paint event i.e protected override void OnPaint(PaintEventArgs e) { e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; using (SolidBrush b = new

              "Creating and destroying drawing objects like pens and brushes is very cheap, takes about a microsecond."
              -Hans Passant

              And:
              In our application we have a few GDI+ objects that are used often in many different contexts. This includes some instances of Font, SolidBrush (White, Black...), some Pen... For those objects our

              "Caching System.Drawing objects is a mistake. They are very cheap to create, very expensive to keep around. They are allocated on a special heap that all processes on a desktop need to share. The heap size is limited to 65535 objects. Creating objects like a brush or a pen takes roughly a microsecond, miniscule compared to the cost of the operation you perform with them. The only drawing object that is expensive is a Font."
              -Hans Passant

              And:
              I have an application that does a lot of drawing, let's pretend it's a Viso-like application. It has objects that have multiple sub-objects that are drawn, things can be connected, resized etc. Cur...

              Comment


                #8
                overflowing,

                Thank you for the additional information.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by frankthearm, Yesterday, 09:08 AM
                14 responses
                47 views
                0 likes
                Last Post NinjaTrader_Clayton  
                Started by gentlebenthebear, Today, 01:30 AM
                2 responses
                13 views
                0 likes
                Last Post gentlebenthebear  
                Started by Kaledus, Today, 01:29 PM
                3 responses
                9 views
                0 likes
                Last Post NinjaTrader_Jesse  
                Started by PaulMohn, Today, 12:36 PM
                2 responses
                17 views
                0 likes
                Last Post PaulMohn  
                Started by Conceptzx, 10-11-2022, 06:38 AM
                2 responses
                56 views
                0 likes
                Last Post PhillT
                by PhillT
                 
                Working...
                X