Trapping an Abnormal termination/timeout message

 

Hi, 

How can I know which part of the code is causing this Abnormal termination/timeout message?  

3       07:01:15.111    EA EURUSD.lmx,M1: shutdown by timeout
1       07:01:23.244    EA EURUSD.lmx,M1: Abnormal termination
0       07:01:23.244    EA EURUSD.lmx,M1: uninit reason 9

It seems a task was aborted when I've shut down the terminal. It's not very often but I recently have this kind of termination, I supposed it's caused by one of the latest modifications.

I would like to code something like the following lines:

if (IsStopped())
   {Print("EA STOPPED: __FUNCTION__ = ",__FUNCTION__,"  __LINE__ = ",__LINE__,"  (compilation time: ",__DATE__," ",__DATETIME__,")");
   }

But I'm not sure where it must be coded, the deinit() function is apparently not called when aborting. Is it enough to code it as the first instruction of the start() function for it to be executed when the _StopFlag will be set to true? 

Is there a function to trap that event and get this lines to be executed? 

 

I assume it's an indicator - not an EA and not a script.

If you e.g. place an endless loop in an indicator the terminal realizes that this indicator is  'not responding' and terminates it - I guess this (not responding) is the reason.

The reason for this 'not responding' is part the code - but without code ... (you know what I mean).

 
jacques366: Is it enough to code it as the first instruction of the start() function for it to be executed when the _StopFlag will be set to true?

No, if the terminal is stopping, then start will not be called.

You must find your infinite loop and fix it. If any of your loops/sleeps/web requests take any longer then 3 seconds your code will be terminated.

 

This problem is coming from the EA not an Indicator. I've traced the called indicators running time and there are OK. It's a big program that I manage I cannot ask decently someone to solve that problem for me, it will take him too much time.  I understand the problem of a loop that is still running, what I would like to find is a system to locate it by for example knowing the line number of the current instruction when the stop occurs. Is it possible? a idea? 

Do you think that doing the following would work? :

put the code of my Start() function into a procedure that I call using

while( !IsStopped() and TimeRun = 0 ) 

 

(in the procedure I set TimeRun = 1 to run the code only once as in the Start function now.

Then just after the while I code the

if (IsStopped())

   {Print("EA STOPPED: __FUNCTION__ = ",__FUNCTION__,"  __LINE__ = ",__LINE__,"  (compilation time: ",__DATE__," ",__DATETIME__,")");

   }

It's a bit tricky, too much in fact, that's why I prefer asking before trying this and I am not sure it will work, there not a lot info on to trap that event.

Will it work? Another idea ? 

 

No it will not. You must find your infinite loop and fix.

Place a print at the start and exit of every function and track it down to a function. then print the variables inside every loop and a exit message from every loop. and track it down.

Stop being lazy and looking for a quick fix. There isn't any.

 

Yes I arrived to the same conclusion.

Thank you all

 

That was in those instructions:

   chartOpen = false;
   long currChart = ChartFirst();
   while(currChart>0)
       {if (ChartSymbol(currChart) == OOrderSymbol[curOrder])          
           {chartOpen = true;
            break;
           }
        currChart=ChartNext(currChart);
       }  

 I solved this problem easily but I don't see why this instructions are creating a never ending loop that is finished when I close the terminal with a timeout.

And why, if it is looping without going out of the while, the timeout is not occurring before I stop the terminal? I thought the start() is stopped by the system when it last too long.   

Apparently there is something I don't get. 

 
There is nothing obviously wrong with that code to cause an endless loop. Is it running inside a bigger loop?
 
jacques366: I thought the start() is stopped by the system when it last too long.  
  1. It is. That is what "shutdown by timeout" means.
  2. What does ChartNext - Chart Operations - MQL4 Reference return when there are no more charts, and what are you checking for?



 

I am writing in a trace file just before and just after the while and when there is a timeout the second write is not done. I've got many cases and all are ending the same way. Pretty sure this comes from that loop...... but I will make sure again.

I trace more info I now I know this is occurring only when I stop the terminal and not if I let the EA running without changing anything (without changing the timeframe I mean). This is occurring at the time I stop the terminal. (I first thought the loop was running and was stopped by my intervention but it's not the case).  

So now the question is why is it occurring at that special moment? Stopping the terminal may change something that is causing this While loop not to end correctly  ?

I still on that case ...

 
jacques366:

I am writing in a trace file just before and just after the while and when there is a timeout the second write is not done. I've got many cases and all are ending the same way. Pretty sure this comes from that loop...... but I will make sure again.

I trace more info I now I know this is occurring only when I stop the terminal and not if I let the EA running without changing anything (without changing the timeframe I mean). This is occurring at the time I stop the terminal. (I first thought the loop was running and was stopped by my intervention but it's not the case).  

So now the question is why is it occurring at that special moment? Stopping the terminal may change something that is causing this While loop not to end correctly  ?

I still on that case ...

It's difficult to answer without knowing the context where this code is used. In which event handler ? Is it nested deeply in other functions call ?

At first I can't see a reason. Can you provide a test code which we could use to reproduce the issue ?

Reason: