Pens and Brush objects

Public support forum for peer to peer support with related to the Visual Objects and Vulcan.NET products
Post Reply
User avatar
GlenT
Posts: 33
Joined: Fri Sep 25, 2015 7:35 pm

Pens and Brush objects

Post 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
User avatar
Chris
Posts: 4922
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Pens and Brush objects

Post 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.
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
User avatar
GlenT
Posts: 33
Joined: Fri Sep 25, 2015 7:35 pm

Pens and Brush objects

Post 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
User avatar
Chris
Posts: 4922
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Pens and Brush objects

Post 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...
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
User avatar
robert
Posts: 4529
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Pens and Brush objects

Post 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
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
GlenT
Posts: 33
Joined: Fri Sep 25, 2015 7:35 pm

Pens and Brush objects

Post 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
User avatar
GlenT
Posts: 33
Joined: Fri Sep 25, 2015 7:35 pm

Pens and Brush objects

Post 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
User avatar
Chris
Posts: 4922
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Pens and Brush objects

Post 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#)
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
User avatar
wriedmann
Posts: 3757
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Pens and Brush objects

Post 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
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
GlenT
Posts: 33
Joined: Fri Sep 25, 2015 7:35 pm

Pens and Brush objects

Post 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
Post Reply