I'm creating a indicator that recognizes candlestick shapes.
To do that I created a separate class Candlestick
that I include to the indicator file.
The problem is that I suffer from memory leaks.
I'm new to pointers and after reading / watching a lot, I still seem to miss something here.
This is the Indicator class. The content of the Candlestick
class is irrelevant so I leave that out.
Candlestick *candles[];
void OnDeinit(const int reason)
{
for(int i = 0; i < ArraySize(candles); i++ ){
delete(candles[i]);
}
}
int OnCalculate(args here)
{
ArrayResize(candles, Bars);
for(int i = MathMax(Bars-2-IndicatorCounted(), 1); i >= 0; i--)
{
candles[i] = new Candlestick();
// Do stuff with this candle (and other candles) here e.g.
if(candles[i+1].type == BULLISH) Print("Last candle was Bullish");
}
}
When I do this I get memory leak errors. It seems that I need to delete the pointers to the candles in that dynamic array. The problem is, when and where? Because I need them in the next iteration of the for(){...}
loop. So I can't delete it there.
When I delete it in the OnDeinit()
function there are still candles out there and I still get the leak error.
How come?
答案 0 :(得分:1)
首先,尼克,欢迎来到{strong> MQL4
您可能已经意识到, MQL4
代码不是 C
。
在许多重要的差异中,关键在于代码执行平台( MetaTrader Terminal 4 )在什么时候做什么。
OnCalculate()
是一个类似僵尸的过程,被多次调用,但无论如何,绝对不在您的控制之下
接下来,OnCalculate()
副设计并不意味着新的Bar
。
MQL4
概念上源于几天,当时计算资源的许多订单更小,而且在代码执行阶段的时间共享CPU-MUX方面要贵得多。
因此,MQL4
- 用户域语言保留了一些无法直接访问的隐藏宝石的好处。其中之一是非常有效的基于寄存器的更新处理,并在miminum上保持动态资源分配,因为它们对实时执行可预测性具有破坏性的不利影响。
这将有助于您了解如何设计&amp;通过模仿 &#34;石器时代&#34; - 但非常高效行为(时间和记忆)来处理您的概念对象更聪明-wise),而不是在每次调用OnCalulate()
时使用无限量的非托管实例充斥你的内存池,这些实例会消耗无数的 new Candlestick(); // *--> candles[]
如果有疑问,只需在平台localhost-help / documentation中阅读 ArrayResize()
的最佳实践,开始实现这些内容,即引入开销(如果不是块)域名,nano$econd$
计数&amp;在专业软件设计中受到伤害。