Canvas is cool! - page 90

 
Nikolai Semko #:

A canvas is an object to which an array of pixels is bound. The Resource is responsible for binding this array of pixels (see bool CCanvas::Create())
It is bad practice to delete and recreate a canvas all the time.
It is good practice to create a canvas when it is needed and delete it when it is no longer needed, e.g. at the end of the programme.

Once you create a canvass object, you can clean it up, overwrite the pixel array every frame, resize the canvass and move it anywhere you want.

Thanks, it became a little clearer, but not all. Do I understand correctly that if after creating a canvas object, I simply delete the object from the chart using ObjectDelete(), then the pixel array bound to this object remains untouched and... When I create a new canvas object with a different name again, a new pixel array is created in memory, and thus, theoretically, it is possible to fill the whole memory?

Or is the old pixel array simply re-bound to the last created canvas-object each time (because it is and only with it that all functions of the used instance of the canvas-class start working)?

 
leon_17 #:

Thanks, it became a little clearer, but not all. Do I understand correctly that if after creating a canvas object, I simply delete the object from the chart using ObjectDelete(), then the pixel array bound to this object remains untouched and.... When I create a new canvas object with a different name again, a new pixel array is created in memory, and thus, theoretically, it is possible to fill up all memory?

Of course, ownerless resources will multiply.

leon_17 #:

Or still the old pixel array is simply re-bound to the last created kanvas-object every time (because it is and only with it that all functions of the used instance of the kanvas-class start working)

Your imagination is superb. I envy you. I would not have thought of such a thing. :))

However, if you save the name of an ownerless resource, you can re-bind it to a new bitmap object. But this is from the field of schizophrenia.

 
Nikolai Semko #:

However, if you save the name of an ownerless resource, you can re-bind it to a new bitmap object. But this is from the field of schizophrenia.

I take my words back. I think I found a case when such a thing could be useful.
 
Nikolai Semko #:
I take that back. I think I found a case where this might be useful.

Thanks, I just wanted to understand what happens in memory if you use ObjectDelete () instead of Destroy (). And I realised that in such a case graphic resources (ownerless pixel arrays and whatever else) will be multiplied if a new name is generated each time for a new canvas object.

And if I use ObjectDelete(), but then create a canvas object with the same name, will the pixel array be old or still new?

p.s. the questions are probably stupid, but I need it to understand the mechanism of canvas


.

 
leon_17 #:

Thanks, I just wanted to understand what happens in memory if you use ObjectDelete () instead of Destroy (). And I realised that graphical resources (ownerless pixel arrays and something else) will multiply if a new name is generated for a new canvas object each time.

It is easy to check this by writing a simple script
 
Nikolai Semko #:
It's easy to check by writing a simple script.

Can you tell me how?

 
leon_17 #:

Can you tell me how?

like this:

#define protected public
#include <Canvas\Canvas.mqh>
#undef protected

void OnStart() {
   while(!IsStopped()) {
      int Width =(ushort)ChartGetInteger(0,CHART_WIDTH_IN_PIXELS);  // получаем Ширину окна
      int Height=(ushort)ChartGetInteger(0,CHART_HEIGHT_IN_PIXELS); // получаем Высоту окна
      CCanvas canvas;
      if(!canvas.CreateBitmapLabel("MyCanva"+string(rand()),0,0,Width,Height,COLOR_FORMAT_ARGB_NORMALIZE)) {
         Print("Error creating canvas: ",GetLastError());
      }
      canvas.FillRectangle(rand()%Width/2,rand()%Height/2,Width/2+rand()%Width/2,Height/2+rand()%Height/2,ARGB(128,rand()%255,rand()%255,rand()%255));
      canvas.Update();
      Comment("Задействовано памяти: - " + string(TerminalInfoInteger(TERMINAL_MEMORY_USED))+ " Mb");
      Sleep(500);
      ObjectDelete(canvas.m_chart_id,canvas.m_objname);
   }
}

and look at the comment:

!!! after running this script, don't forget to reload the terminal to free up memory for orphan resources.


 
Nikolai Semko #:

like this:

and look at the comment:

!!! after running this script, don't forget to reload the terminal to free up memory for orphan resources.


Thank you very much for your help! I have not worked with memory in this way before, I will test it.
Thanks for the hint that orphan resources are deleted when reloading the terminal! Many such things are not obvious to me yet.

p.s. everything in this example is perfect! There is something to look at, something to think about and an opportunity to experiment... The code is short.

 
Can you tell me more, if kanvas is used in an Expert Advisor, is it necessary to execute Destroy() when removing the Expert Advisor from the chart and closing the terminal? I don't know how to check this either. I mean a working canvas with a fixed object. There the difference in memory is within the error (if you do and do not perform Destroy().
 
leon_17 removing the Expert Advisor from the chart and closing the terminal? I don't know how to check this either. I mean a working canvas with a fixed object. There the difference in memory is within the error (if you perform and do not perform Destroy().

You don't need to. In the Canvas destructor it is automatically executed:

                    ~iCanvas() { Destroy(); ChartRedraw();};