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

Error OnRender: Attempted to read or write protected memory

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

    Error OnRender: Attempted to read or write protected memory

    Hello,

    I have an indcator that I've been working on for a while. My prior post was asking how to create a LinearGradientBrush to which I got the info for. Now I'm trying to implement it OnRender and I'm getting an error message saying "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

    This is the code I'm running:

    Code:
    private LinearGradientBrush linGrBrush;
    private SharpDx.Direct2D1.Brush gradientBrush;
    private SharpDx.RectangleF reuseRect;
    
    protected override void OnRender (ChartControl chartControl, ChartScale chartScale)
    {
         base,OnRender(chartControl, chartScale);
    
         foreach(KeyValuePair<int, SortedDictionary<double,long>> historicdict in rowsArray)
         {
              foreach(KeyValuePair<double,long> keyValue in historicdict.Value)
              {
                   xpos = chartControl.GetXByBarIndex(ChartBars, histroicdict.Key +1)
    
                   if (RenderTarget != null)
                   {
                        linGrBrush = new LinearGradientBrush(System.Windows.Media.Color.FromRgb(255,255,0), System.Windows.Media.Color.FromRgb(0,128,255),
                        new System.Windows.Point(xpos,0), new System.Windows.Point(xpos-((int)(chartControl.Properties.BarDistance * 20))));
                        gradientBrush = linGrBrush.ToDxBrush(RenderTarget);
                   }
    
                   UpdateRect(ref reuseRect, xpos, ymid, xpos-((int)(chartControl.Properties.BarDistance * 20)), height)
                   RenderTarget.FillRectangle(reuseRect, gradientBrush)
    
                   if (gradientBrush != null)
                   {gradientBrush.Dispose();}
              }
         }
    }

    How can I resolve this issue? It is causing my NT to stop working and the entire application crashes.

    #2
    Hello KINGKODA,

    Thank you for your post.

    It looks from the code you've posted that you're creating these outside of OnRender(), is that correct?
    private LinearGradientBrush linGrBrush; private SharpDx.Direct2D1.Brush gradientBrush; private SharpDx.RectangleF reuseRect; SharpDX Brushes are device-dependent resources, which means they can only be used with the device (i.e., RenderTarget) which created them. In practice, this mean you should ONLY create your SharpDX brushes during the chart object's OnRender() or OnRenderTargetChanged() methods. Not doing this can lead to a host of issues including memory and application corruption which can negatively impact the stability of NinjaTrader and lead to crashing as you've seen.

    Here's a link to our help guide that goes over using SharpDX for custom rendering:



    Please let us know if we may be of further assistance to you.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Kate, can you tell why you referred the OP's code as "creating outside of OnRender"?

      In that example, the brushes are declared in class scope, but created in OnRender.

      How that behavior is called as "creating outside of OnRender"?
      if that's right, then "OnRenderTargetChanged" will be also "creating outside of OnRender", because the variables will still need to be declared in class scope? like:

      Code:
      private .. myDxBrush;
      
      ...OnRenderTargetChange()
      {
      ...
      myDxBrush = ...
      ...
      }
      
      ... OnRender()
      {
      ...
         do something with myDxBrush
      ...
      }

      I cant understand.
      Last edited by ttodua; 11-22-2019, 10:59 AM.

      Comment


        #4
        Hello ttodua,

        Thank you for your reply.

        The problem is declaring the SharpDX brushes outside of OnRender() or OnRenderTargetChanged. You can declare WPF brushes at the class level and then convert them to a SharpDX brush in OnRender, but if you declare SharpDX brushes at the class level you run into threading issues.

        I'd advise that you take a look at the built in SampleCustomRender indicator's code as it contains wealth of information regarding rendering and working with both WPF and SharpDX brushes. This is especially important in version 19 and above as this is now enforced and will throw an error if you do not create your SharpDX resources in the chart's UI thread.

        Please let us know if we may be of further assistance to you.
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          I have checked few times in my script where I am not using SharpDX brushes outside onRender or OnRenderTargetchanged, but still getting similar error. Can you help?

          Comment


            #6
            Hello asmmbillah,

            Thank you for your reply.

            Would you be able to post a snippet of code? Without seeing what you're doing it would be extremely difficult to offer any advice.

            Thanks in advance; I look forward to assisting you further.
            Kate W.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by tkaboris, Today, 05:13 PM
            0 responses
            2 views
            0 likes
            Last Post tkaboris  
            Started by GussJ, 03-04-2020, 03:11 PM
            16 responses
            3,281 views
            0 likes
            Last Post Leafcutter  
            Started by WHICKED, Today, 12:45 PM
            2 responses
            19 views
            0 likes
            Last Post WHICKED
            by WHICKED
             
            Started by Tim-c, Today, 02:10 PM
            1 response
            10 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by Taddypole, Today, 02:47 PM
            0 responses
            5 views
            0 likes
            Last Post Taddypole  
            Working...
            X