Errors, bugs, questions - page 1785

 
fxsaber:
I know what the destructor is for. It's not clear to me why the word virtual should be written before its definition.
I need it for code compatibility with C++, because destructors are different there
 
A100:
I need it for code compatibility with C++, because destructors are different there
Got it, thanks. Do you port to MQL5 a lot? - Is that why the examples are so specific?
 
fxsaber:
I know what the destructor is used for. I don't understand why we have to add the word virtual before its definition.

Virtual functions and destructor >>>

The basic rule: if you have at least one virtual function in your class, the destructor should also be virtual. Remember that the destructor is not virtual by default, so you should declare it explicitly. If you don't do this, you are almost certain to have memory leaks in your program. Again it doesn't take much intelligence to understand why. Let's look at several examples.

 
fxsaber:
Got it, thanks. Do you port to MQL5 a lot? - Is that why the examples are so specific?
I don't port a lot. Specific only at first glance. Some "experts" wrote here that more than 3 derivative classes is harmful. I often have only 3-4 base classes that need proper initialization and only then the real derivatives come.
 
int main()
{
    A * pA = new B;
    delete pA;
    return EXIT_SUCCESS;
}

This time the object is constructed as it should be, but when it is destroyed, a memory leak occurs, because the destructor of the derived class is not called.

The reason is that deletion is performed through a pointer to a base class and the compiler uses early binding to call the destructor. The destructor of the base class cannot call the destructor of the derived class because it does not know anything about it. As a result, some of the memory allocated for the derived class is irretrievably lost.

To avoid this, the destructor in the base class must be declared as virtual.

I don't understand architecturally why, by making the base destructor virtual, information about the derived destructor appears.

The way I understand it now is that when the base is virtual, by defining a derivative destructor we make a replacement for the derivative destructor instead of the base destructor. But then the base destructor should not be called in this example.

Obviously, I don't understand how this works. I have never had problems with virtual functions in MQL5. I like a lot of OOP because of them. I thought I understood it all. Now I have a complete dilemma. Please explain it to me on your own terms.

 
A100:
Portraying little. Specific only at first glance. Some "experts" wrote here that EVERYTHING more than 3 derivative classes is harmful. I often have only 3-4 base classes to properly initialize and only then the real derivatives start
I'd like to see an example of how professionals (from my point of view) write in MQL5.
 
fxsaber:

But then the base destructor should not be called in this example.

It is called by the derived destructor implicitly. This is actually what makes it different from simple virtual functions.
 
Комбинатор:
It is called by the derived destructor implicitly. That's actually what makes it different from simple virtual functions.
Thank you. It turns out that the virtual destructor is not just a virtual function that is called while an object is being destroyed, but some more tricky entity.
 
fxsaber:
Well he has to delete the object) that's the tricky part
 
Alexey Kozitsyn:

SymbolInfoTick() error returning irrelevant data - tick overshoot. Build 1525 x64. Test from indicator, did not test on Expert Advisor:

Real, Opening, Access Server V. Sadness! Pay attention to the problem, please. And also to the problem in application#1598238 (starting from page 10, post of2017.01.26 09:43).

Dear developers, let's fix the ticks! Code provided, here it is - the problem, no need to look for anything, just react and fix it!

If we replace OnCalculate with OnTick, no errors occur. The problem is not in SymbolInfoTick, the problem is that indicators are crookedly configured not to skip.
Reason: