Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Request: Implement System.IDisposable on SimpleFont()

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

    Request: Implement System.IDisposable on SimpleFont()

    Many of us routinely dispose of fonts that we create on the fly, such as when using any of the Text drawing methods, so we need SimpleFonts to be disposable, just as Fonts are.

    #2
    The SimpleFont uses complete managed .NET resources so they should be managed by the GC.

    Our developers are curious why you feel you would need to dispose of these objects? Are you seeing instances where references are being held on to?
    MatthewNinjaTrader Product Management

    Comment


      #3
      Originally posted by NinjaTrader_Matthew View Post
      The SimpleFont uses complete managed .NET resources so they should be managed by the GC.

      Our developers are curious why you feel you would need to dispose of these objects? Are you seeing instances where references are being held on to?
      The very first example shows how I used to use Font objects, in for example, DrawText() etc.. (I know that DrawText() is NT7, and we have a new situation in NT8. I just need to know if I have to dispense with using a syntax that automatically disposes of the Font object?).

      ref: https://msdn.microsoft.com/en-us/library/yh598w02.aspx

      Are we saying that for SimpleFont, this statement about managed types accessing unmanaged resources, such as device contexts does not apply?
      File and Font are examples of managed types that access unmanaged resources (in this case file handles and device contexts). There are many other kinds of unmanaged resources and class library types that encapsulate them. All such types must implement the IDisposable interface.

      Comment


        #4
        Originally posted by koganam View Post
        Are we saying that for SimpleFont, this statement about managed types accessing unmanaged resources, such as device contexts does not apply?
        Yes - All of the objects used in SimpleFont are managed, therefore they will be picked up by the garbage collector. There should be no reason to need to dispose of a managed resource.

        Objects like Font which wrap unmanaged resources do need to be disposed of as the GC is not aware of them. However SimpleFont does not use the Font class referenced in that MSDN article, therefore you do not need to dispense these SimpleFont objects for something like Draw.Text()

        The exception would be if you're using your own SharpDX.TextFormat from a SimpleFont

        Code:
        SharpDX.DirectWrite.TextFormat	textFormat 	= myFont.ToDirectWriteTextFormat();
        In this case, you would need to dispose of the unmanaged resources used in this object
        Code:
        NinjaTrader.Gui.Tools.SimpleFont myFont = new NinjaTrader.Gui.Tools.SimpleFont("Arial", 12);
        
        using (SharpDX.DirectWrite.TextFormat  textFormat	 = myFont.ToDirectWriteTextFormat())
        {
        	textFormat.FlowDirection = SharpDX.DirectWrite.FlowDirection.TopToBottom;
        }
        Last edited by NinjaTrader_Matthew; 06-08-2015, 03:44 PM.
        MatthewNinjaTrader Product Management

        Comment


          #5
          Originally posted by NinjaTrader_Matthew View Post
          Yes - All of the objects used in SimpleFont are managed, therefore they will be picked up by the garbage collector. There should be no reason to need to dispose of a managed resource.

          Objects like Font which wrap unmanaged resources do need to be disposed of as the GC is not aware of them. However SimpleFont does not use the Font class referenced in that MSDN article, therefore you do not need to dispense these SimpleFont objects for something like Draw.Text()

          The exception would be if you're using your own SharpDX.TextFormat from a SimpleFont

          Code:
          SharpDX.DirectWrite.TextFormat	textFormat 	= myFont.ToDirectWriteTextFormat();
          In this case, you would need to dispose of the unmanaged resources used in this object
          Code:
          NinjaTrader.Gui.Tools.SimpleFont myFont = new NinjaTrader.Gui.Tools.SimpleFont("Arial", 12);
          
          using (SharpDX.DirectWrite.TextFormat  textFormat	 = myFont.ToDirectWriteTextFormat())
          {
          	textFormat.FlowDirection = SharpDX.DirectWrite.FlowDirection.TopToBottom;
          }
          (Low priority). Is there any chance that we can get a listing of what objects will need to be Dispose()d?

          Comment


            #6
            Yes, we can provide that in the documentation.

            From the top of my head, common objects you would need to dispose of include:

            - NinjaTrader.Gui.Stroke
            - SharpDX.Direct2D1.Brush
            - SharpDX.DirectWrite.TextLayout
            - SharpDX.DirectWrite.TextFormat
            - SharpDX.Direct2D1.PathGeometry

            There are others in the SharpDX library that you may come across (bitmaps, etc), but these are the most common objects you'll come across for general chart rendering.
            MatthewNinjaTrader Product Management

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by DJ888, 04-16-2024, 06:09 PM
            4 responses
            12 views
            0 likes
            Last Post DJ888
            by DJ888
             
            Started by terofs, Today, 04:18 PM
            0 responses
            8 views
            0 likes
            Last Post terofs
            by terofs
             
            Started by nandhumca, Today, 03:41 PM
            0 responses
            6 views
            0 likes
            Last Post nandhumca  
            Started by The_Sec, Today, 03:37 PM
            0 responses
            3 views
            0 likes
            Last Post The_Sec
            by The_Sec
             
            Started by GwFutures1988, Today, 02:48 PM
            1 response
            9 views
            0 likes
            Last Post NinjaTrader_Clayton  
            Working...
            X