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

State.Terminated - RC2

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

    State.Terminated - RC2

    I open 2 tabs with my indicator then connect to data feed
    Debug log

    47111791 OnRender - start
    47111791 OnRender - end
    47111791 OnRender - start
    47111791 OnRender - end
    47111791 OnRender - start
    47111791 STATE: Terminated 8.152 sec
    Indicator 'MyIndicator': Error on calling 'OnRender' method on bar 0: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

    6963748 STATE: SetDefaults
    6963748 OnRenderTargetChanged - creating graphics

    No other errors before this one in OnRender
    I used lock(obj) {} in OnStateChange() and in OnRender() and this helped solve issue.
    I suppose OnStateChange and OnRender executed in different threads and they are not sync on termination.

    I still had exceptions when on multi tabs chart. I suppose it happened more often on slow PCs. Exceptions have been listed here
    http://ninjatrader.com/support/forum...348#post482348
    Last edited by ren37; 11-12-2016, 10:27 AM.

    #2
    Hello ren37,

    Are there any indicators on these charts?

    Are you able to reproduce the errors with no indicators applied?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hello ren37,

      Are there any indicators on these charts?

      Are you able to reproduce the errors with no indicators applied?
      Hello

      Yes, of course 'MyIndicator' indicator is on those 2 tabs (else how I get this log?).
      Can you tell me why it trminated before end OnRender()? All additional info is in my first post. I can't provide source to you because it's some business project. But you can try reproduce this by disposing any object in on State.Terminated case and use this object in OnRender(). Or something like that.


      private Object obj = new Object();

      ...
      case State.Terminated:
      obj = null;
      break;
      ...

      OnRender()
      {
      Sleep(500);
      Print(obj.ToString());
      }

      Comment


        #4
        Originally posted by ren37 View Post
        Hello

        Yes, of course 'MyIndicator' indicator is on those 2 tabs (else how I get this log?).
        Can you tell me why it trminated before end OnRender()? All additional info is in my first post. I can't provide source to you because it's some business project. But you can try reproduce this by disposing any object in on State.Terminated case and use this object in OnRender(). Or something like that.


        private Object obj = new Object();

        ...
        case State.Terminated:
        obj = null;
        break;
        ...

        OnRender()
        {
        Sleep(500);
        Print(obj.ToString());
        }
        OnStateChange() does run async, so you can definitely run into situations where your terminated object runs in-flight with other event methods.

        For this reason, you should make sure to conditionally check for null reference on these objects before accessing them.

        Our development team is aware of the implications of this design decision and we are currently monitoring and tracking feedback/incidents under SFT-1247. I have included your report as an incident for this request.

        Also, a few other observations from your script:

        I would not recommend instantiating that object as a class level variable but rather create the new reference during State.Configure. Doing so will ensure objects are setup only as the indicator has actually been configured, and not because of some other user action. You can also consider adding a flag in your State.Terminated logic to help filter certain actions as advised here when tearing down the objects: http://ninjatrader.com/support/helpG...fecycle_of.htm

        It is also not recommend to Sleep any NinjaTrader events. If you are just using this to demonstrate some problem, I understand - but in production, please avoid the use.
        MatthewNinjaTrader Product Management

        Comment


          #5
          Originally posted by NinjaTrader_Matthew View Post
          For this reason, you should make sure to conditionally check for null reference on these objects before accessing them.
          This garanties nothing in multi-threading apps.
          I used lock() operator (see my first post). This issue solved but some more are not.

          NT now has pretty complex multi-threading implementation related to custom indicators.
          But what about State.Terminated this is not described correct nowhere in NinjaScript help.

          Still got sporadic D2D errors. Perhaps it's not a good idea to follow help guide and reinit graphics in OnRenderTargetChanged(). Who knows what happened inside?..
          Last edited by ren37; 11-14-2016, 01:09 PM.

          Comment


            #6
            I am doing more research and will get back to you on these concerns.
            MatthewNinjaTrader Product Management

            Comment


              #7
              Originally posted by ren37 View Post
              This garanties nothing in multi-threading apps.
              I used lock() operator (see my first post). This issue solved but some more are not.

              NT now has pretty complex multi-threading implementation related to custom indicators.
              But what about State.Terminated this is not described correct nowhere in NinjaScript help.

              Still got sporadic D2D errors. Perhaps it's not a good idea to follow help guide and reinit graphics in OnRenderTargetChanged(). Who knows what happened inside?..
              And yet another one bites the dust!

              You are using State.Terminated to clean up, as every reasonable developer would and should. You are being killed by unintended side-effects that so far are not going to be properly fixed.

              ref: http://ninjatrader.com/support/forum...ate.terminated

              There is a hacky, kludgy, workaround somewhere in that thread, that NT has decided to adopt as a solution instead of fixing the design.

              Comment


                #8
                Koganam,

                Your case is different. Has to do with State.Termination running for both the NinjaScript in the object dialog and also on the chart and knowing that you only need to cleanup items based on what state NinjaScript had reached. This is different then NT7 since in NT7 we didn't run OnTermination for objects in the dialog and never gave developers a chance to properly cleanup NinjaScript instance generated for the object dialog. With NT8 we're now more correct and strictly follow state management for all instances, even those in the object dialog.

                Ren37,

                Your case is exceptions you see as you try to clean up resources in State.Terminated but as NT is multi threaded there is a chance that resource is in use while its being cleaned up resulting in an exception that is logged. In this case we have worked with development and to check into the case. This will result in code changes for 8.0.2.0 which will be that if an exception occurs while a NinjaScript object is in terminated state then exceptions generated will be ignored and not sent to the log for alarm. Since there is no alarm the NinjaScript that is being shutdown.

                This was in place already through the majority of the product but was missing in a few locations like 'OnRender' like you reported.

                The workaround for now is to add a try/catch block around your NinjaScript code in OnBarUpdate, OnRender, anywhere else you have code implemented that is as follows. This is the same code we will add just one level higher above your user created code.

                Code:
                try
                				{
                					// Your code here
                				}				
                				catch (Exception ex)
                				{
                					if (State >= State.Terminated)
                					{
                					 //Ignore this since NinjaScipt is shutting down, we don't care...
                					}
                					else
                					{
                					 Log("Exception " + ex.InnerException, LogLevel.Error); 
                					}					
                				}
                Last edited by NinjaTrader_Brett; 11-18-2016, 04:52 PM.

                Comment


                  #9
                  Why just not to use lock() operator?

                  Code:
                          protected override void OnStateChange()
                          {
                              lock(lockObj)
                              {  ...
                  
                  
                          protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
                          {
                                  lock (lockObj)
                                  {  ...
                  This works with no problem and exceptions.
                  And try-catch {} operator is more resorce consuming.

                  Comment


                    #10
                    We have a try/catch block there anyways to catch user exceptions so we would prefer to reuse the concept instead introducing a new lock concept here. Note: You can use lock for now to avoid the errors (haven't tried myself but should do the trick) or just ignore them from the log until next NT8 release is out which will implement what I mentioned below.

                    -Brett

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by algospoke, Yesterday, 06:40 PM
                    2 responses
                    23 views
                    0 likes
                    Last Post algospoke  
                    Started by ghoul, Today, 06:02 PM
                    3 responses
                    14 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Started by jeronymite, 04-12-2024, 04:26 PM
                    3 responses
                    45 views
                    0 likes
                    Last Post jeronymite  
                    Started by Barry Milan, Yesterday, 10:35 PM
                    7 responses
                    22 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Started by AttiM, 02-14-2024, 05:20 PM
                    10 responses
                    181 views
                    0 likes
                    Last Post jeronymite  
                    Working...
                    X