Incomprehensible behavior on OnDeinit when saving objects... - page 2

 
Alain Verleyen #:

I do agree the main problem is the design itself, it's a bad idea to save objects only in OnDeinit().

Though about the OP issue itself, I am wondering what is the source of this statement : "The EA will be notified of the deinit event after the chart has been destroyed by the terminal. " ?

Certainly when the user said the issue happened when changing the timeframe, so the chart should not be destroyed at all.

I don't know, I am staying open as long as there is not a clear explanation.
Actually, I was thinking about that statement as well, because the objects usually exist on other time-frames as well... It seems though, the chart context is not available for some time, and that's why there is an error in accessing the objects. - Also the error reported is indicative for a NULL string or an empty string when searching for an object. Thus, the call to receive the name fails, but obviously without error. So my conclusion is, for some time in between the change to the chart, the context is unavailable to such an extend, that the object list is empty, and the return result of retrieving the name of the object comes back with an empty value.

So that's why I concluded, the context of the chart/the chart is destroyed by the terminal.

Edit:
It's unclear to me if this is considered a bug.

But for the OP: To solve the symptomatic issue, I strongly suggest changing the approach in general. OnDeinit is not exactly the right place to do such tasks.
 
Dominik Egert #:
Actually, I was thinking about that statement as well, because the objects usually exist on other time-frames as well... It seems though, the chart context is not available for some time, and that's why there is an error in accessing the objects. - Also the error reported is indicative for a NULL string or an empty string when searching for an object. Thus, the call to receive the name fails, but obviously without error. So my conclusion is, for some time in between the change to the chart, the context is unavailable to such an extend, that the object list is empty, and the return result of retrieving the name of the object comes back with an empty value.

So that's why I concluded, the context of the chart/the chart is destroyed by the terminal.


I see but I can't reproduce it so...
 
Alain Verleyen #:
I see but I can't reproduce it so...
Maybe we should ask for the build version..

 
Yoloic:
Hello programmer fellows.

To preserve the objects on the chart when the settings of my EA or the timeframe is changed, my EA saves the objects on external files on OnDeinit(), and load the objects from the external files on OnInit().


Thank you in advance for your time.

Please tell us the build version of your terminal...
 
Alain Verleyen #:
I see but I can't reproduce it so...
The documentation says: "before a re-initialization due to the change of a symbol or a chart period the mql5 program is attached to;"

Concerning the calling of the OnDeinit.Actually it is not clear what state the chart will be in when calling. But on the other hand, it was always possible to delete objects in OnDeinit.

So something is not exactly matching up here. Especially when you cannot reproduce.
 

Thank you very much for trying to help.


I use the Version 5.00 - Build 4410.


Another thing that I have noticed, when I put the SaveLines() in the OnTimer() and OnDinit() then it throws the same errors, but when I comment the SaveLines() from the OnDeinit() and just leave the one from the OnTimer() it works correctly and the lines are loaded during OnInit().


So it seems that the issue comes from the OnDeinit function, and more specifically from the REASON_CHARTCHANGE reason.


The very same code is working correctly when the deinit reason is:

REASON_RECOMPILE

REASON_PARAMETERS

REASON_CHARTCLOSE

REASON_CLOSE


I guess I could leave the SaveLine() in the OnTimer() and remove it from the OnDeinit() for now as I'm not familiar with sqlite database (thank you for the suggestion Dominik).


Also, why is it a bad practice to save the object on OnDeInit?

I don't have any reason to save them while the EA is running that's why I put the function in the OnDeinit function thinking it was the more appropriate place to do it as I want to load them on OnInit() but both of you suggested that it's not a good idea so I'm surprised.

 
Yoloic #:

Thank you very much for trying to help.


I use the Version 5.00 - Build 4410.


Another thing that I have noticed, when I put the SaveLines() in the OnTimer() and OnDinit() then it throws the same errors, but when I comment the SaveLines() from the OnDeinit() and just leave the one from the OnTimer() it works correctly and the lines are loaded during OnInit().


So it seems that the issue comes from the OnDeinit function, and more specifically from the REASON_CHARTCHANGE reason.


The very same code is working correctly when the deinit reason is:

REASON_RECOMPILE

REASON_PARAMETERS

REASON_CHARTCLOSE

REASON_CLOSE


I guess I could leave the SaveLine() in the OnTimer() and remove it from the OnDeinit() for now as I'm not familiar with sqlite database (thank you for the suggestion Dominik).

As already said you need to provide full code to reproduce the issue if you need further help.

Also, why is it a bad practice to save the object on OnDeInit?

I don't have any reason to save them while the EA is running that's why I put the function in the OnDeinit function thinking it was the more appropriate place to do it as I want to load them on OnInit() but both of you suggested that it's not a good idea so I'm surprised.

In general, it's not a good idea to save something important (only) in OnDeinit(). What will happen if MT5 crash or you computer, or a power outage or ...?

 
Yoloic #:

Thank you very much for trying to help.


I use the Version 5.00 - Build 4410.


Another thing that I have noticed, when I put the SaveLines() in the OnTimer() and OnDinit() then it throws the same errors, but when I comment the SaveLines() from the OnDeinit() and just leave the one from the OnTimer() it works correctly and the lines are loaded during OnInit().


So it seems that the issue comes from the OnDeinit function, and more specifically from the REASON_CHARTCHANGE reason.


The very same code is working correctly when the deinit reason is:

REASON_RECOMPILE

REASON_PARAMETERS

REASON_CHARTCLOSE

REASON_CLOSE


I guess I could leave the SaveLine() in the OnTimer() and remove it from the OnDeinit() for now as I'm not familiar with sqlite database (thank you for the suggestion Dominik).


Also, why is it a bad practice to save the object on OnDeInit?

I don't have any reason to save them while the EA is running that's why I put the function in the OnDeinit function thinking it was the more appropriate place to do it as I want to load them on OnInit() but both of you suggested that it's not a good idea so I'm surprised.

Additionally to what Alain said, I am not 100% sure, OnInit will have a chart available in all circumstances.

As said, I am not sure about that, but could imagine, if you fire up the terminal with a template loading the EA, the OnInit could be called before you can draw on the chart.

Using sqlite is quite straightforward in MQL, you use the Database* functions, and you are good to go with that.