Welcome, Guest
Username: Password: Remember me
Visual Objects

Please use this forum to post questions about Visual Objects and Vulcan.NET
  • Page:
  • 1

TOPIC:

Pens and Brush objects 30 Oct 2020 20:27 #16446

  • GlenT
  • GlenT's Avatar
  • Topic Author


  • Posts: 31
  • 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

    Please Log in or Create an account to join the conversation.

    Pens and Brush objects 30 Oct 2020 22:33 #16447

    • Chris
    • Chris's Avatar


  • Posts: 3739
  • 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.
    XSharp Development Team
    chris(at)xsharp.eu

    Please Log in or Create an account to join the conversation.

    Pens and Brush objects 31 Oct 2020 01:09 #16449

    • GlenT
    • GlenT's Avatar
    • Topic Author


  • Posts: 31
  • 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

    Please Log in or Create an account to join the conversation.

    Pens and Brush objects 31 Oct 2020 06:49 #16450

    • Chris
    • Chris's Avatar


  • Posts: 3739
  • 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...
    XSharp Development Team
    chris(at)xsharp.eu

    Please Log in or Create an account to join the conversation.

    Pens and Brush objects 31 Oct 2020 09:14 #16452

    • robert
    • robert's Avatar


  • Posts: 3276
  • 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

    Please Log in or Create an account to join the conversation.

    Pens and Brush objects 31 Oct 2020 20:47 #16455

    • GlenT
    • GlenT's Avatar
    • Topic Author


  • Posts: 31
  • 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

    Please Log in or Create an account to join the conversation.

    Pens and Brush objects 02 Nov 2020 00:19 #16461

    • GlenT
    • GlenT's Avatar
    • Topic Author


  • Posts: 31
  • 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

    Please Log in or Create an account to join the conversation.

    Pens and Brush objects 02 Nov 2020 07:18 #16463

    • Chris
    • Chris's Avatar


  • Posts: 3739
  • 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#)
    XSharp Development Team
    chris(at)xsharp.eu

    Please Log in or Create an account to join the conversation.

    Pens and Brush objects 02 Nov 2020 07:41 #16466

    • wriedmann
    • wriedmann's Avatar


  • Posts: 3244
  • 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

    www.riedmann.it - docs.xsharp.it

    Please Log in or Create an account to join the conversation.

    Pens and Brush objects 02 Nov 2020 10:56 #16472

    • GlenT
    • GlenT's Avatar
    • Topic Author


  • Posts: 31
  • 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

    Please Log in or Create an account to join the conversation.

    • Page:
    • 1