xsharp.eu • Pens and Brush objects
Page 1 of 1

Pens and Brush objects

Posted: Fri Oct 30, 2020 7:27 pm
by GlenT
Hi,

I am using the following code to draw 3 rectangles on a DataWindow,

METHOD Expose(oExposeEvent) CLASS Tab_Gantt
LOCAL oBoundingBox AS BoundingBox
LOCAL oRect AS RectangleObject
LOCAL oOrigin AS Point

oBoundingBox := IIF(oExposeEvent == NULL_OBJECT, NULL_OBJECT, oExposeEvent:ExposedArea)
SUPER:Expose(oExposeEvent)
//Put your changes here
oOrigin := SELF:oCCBtnTodayColour:Origin
oOrigin:X += 110
oRect := RectangleObject{ oOrigin, Dimension{ 29, 21 }, Pen{ Color{ SELF:Owner:aTodayColour[1], SELF:Owner:aTodayColour[2], SELF:Owner:aTodayColour[3] } }, Brush{ Color{ COLORWHITE } } }
SELF:Draw( oRect )
oOrigin := SELF:oCCBtnSelectedColour:Origin
oOrigin:X += 180
oRect := RectangleObject{ oOrigin, Dimension{ 29, 21 }, Pen{ Color{ SELF:Owner:aSelectedColour[1], SELF:Owner:aSelectedColour[2], SELF:Owner:aSelectedColour[3] } }, Brush{ Color{ COLORWHITE } } }
SELF:Draw( oRect )
oOrigin:X += 180
oRect := RectangleObject{ oOrigin, Dimension{ 29, 21 }, Pen{ Color{ SELF:Owner:aSelectedColour[1], SELF:Owner:aSelectedColour[2], SELF:Owner:aSelectedColour[3] } }, Brush{ Color{ SELF:Owner:aSelectedColour[1], SELF:Owner:aSelectedColour[2], SELF:Owner:aSelectedColour[3] } } }
SELF:Draw( oRect )
RETURN NIL


The question is, am I going to run into memory problems not specifically destroying the Pen and Brush objects by doing something like this:

oBrush := Brush{ Color{ SELF:Owner:aSelectedColour[1], SELF:Owner:aSelectedColour[2], SELF:Owner:aSelectedColour[3] } }

.... use it ....

oBrush := NULL_OBJECT


Cheers

Glen Townsley

Pens and Brush objects

Posted: Fri Oct 30, 2020 9:33 pm
by Chris
Hi Glen,

No, there shouldn't be any memory problems, because the garbage collector will collect those objects, sometime after they get out of scope and call their destructor/destroy methods automatically. Only problem would be if the Brush class did not properly release any resources it uses in its destructor code, but from what I see, it does that fine.

Having said that, it's still not a good idea to create that many GUI objects and use resources without this being necessary. It's a lot better to just reuse those objects, for example by declaring the brush var as static, or making it a class var etc.

Pens and Brush objects

Posted: Sat Oct 31, 2020 12:09 am
by GlenT
Thanks Chris. Its been a while, London more than 10 years ago.

I don't think this will be a problem as its for a colour choise for a Gantt Chart so the user will likely only ever use it once.

Cheers

Glen

Pens and Brush objects

Posted: Sat Oct 31, 2020 5:49 am
by Chris
Cheers Glen, been a long time indeed! Regarding using it only once, remember that the Exposed() callback method gets called every single time there's a need to repaint the window, when resizing it, when another window is moving over it, when you alt-tab etc...

Pens and Brush objects

Posted: Sat Oct 31, 2020 8:14 am
by robert
Glen,
What Chris said.
And assigning NULL_OBJECT to clear local reference variables at the end of a method is an urban legend that is really not needed. (also not in VO). When the function/method goes out of scope then the garbage collector will clean up everything.

The only place where that could be of use is in the middle of a method/function when you are done with an object. When the GC runs in the second half of your statement list then the it knows that these objects can be discarded.

The same is true for assigning NULL_OBJECT to class variables in the Axit method. In a Destroy() it may be useful because sometimes the destroy is called manually and later also by the garbage collector.

Robert

Pens and Brush objects

Posted: Sat Oct 31, 2020 7:47 pm
by GlenT
Thanks Chris and Robert. I'm happy that it won't cause issues. It's amazing how these urban legends gain popularity isn't it. I don't think I normally set objects to NULL-OBJECT but I will look for it and remove them as I see them. I do set arrays to NULL_ARRAY in places where I am using possibly big arrays and am worried about memory.

This is all in VO by the way.

Cheers

Glen

Pens and Brush objects

Posted: Sun Nov 01, 2020 11:19 pm
by GlenT
Chris and Robert,

Thinking about this issue over the weekend, I've changed the Pen and Brush objects to be Protected variables of the Data window (which it a sub data window of a DataDialog) . This should see off and problems.

Cheers

Glen

Pens and Brush objects

Posted: Mon Nov 02, 2020 6:18 am
by Chris
Glen, I agree this is the best approach! Especially in VO, where the GC is not perfect...(I originally thought you were talking about .Net/X#)

Pens and Brush objects

Posted: Mon Nov 02, 2020 6:41 am
by wriedmann
Hi Glen,
for similar situations I store these objects in a instance variable of the App object so I can use them for all windows where they are needed.
Wolfgang

Pens and Brush objects

Posted: Mon Nov 02, 2020 9:56 am
by GlenT
Thanks Wolfgang. I would do this too if there were not isolated to one window and I have no plans to put them anywhere else. But things change don't they and I'll keep it in mind.

Cheers

Glen