Page 1 of 1 1
Topic Options
#92299 - 2003-07-02 04:28 PM Object Variables and UDFs
Stevie Offline
Starting to like KiXtart
*****

Registered: 2002-01-09
Posts: 199
Have a quick question about object cleanup. Let's say there's a UDF with a COM object variable (defined in the UDF) that's active. When the UDF completes and returns to the caller, does KiX release the reference to the object and then free up the memory previously consumed by the variable? Or is it necessary to set the object to 0 before leaving the UDF?
_________________________
Stevie

Top
#92300 - 2003-07-02 04:32 PM Re: Object Variables and UDFs
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
If the variable is DIMmed locally then the variable will be destroyed and thus the object released. However, I do consider it best practices to force the objects destroyed as in
code:
function example()
dim $objTest

$objTest=createobject('Kixtart.Form')
; do stuff
$objTest = 0
exit @ERROR
endfunction

_________________________
There are two types of vessels, submarines and targets.

Top
#92301 - 2003-07-02 04:36 PM Re: Object Variables and UDFs
Chris S. Offline
MM club member
*****

Registered: 2002-03-18
Posts: 2368
Loc: Earth
If you Dim the object variable in the function it will remain local to the function. Otherwise, you can still access the object.

Demonstration...

code:
Break On

GetUserName(@WKSTA) ?

For Each $objComputer in $colComputer
$objComputer.UserName ?
Next

Function GetUserName($sComputer)
;Dim $colComputer
$colComputer = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" + $sComputer +
"\root\cimv2").ExecQuery("Select * from Win32_ComputerSystem")
For Each $objComputer in $colComputer
$GetUserName = $objComputer.UserName
Next
EndFunction


Top
#92302 - 2003-07-02 04:37 PM Re: Object Variables and UDFs
Stevie Offline
Starting to like KiXtart
*****

Registered: 2002-01-09
Posts: 199
That's what I have typically done just to make sure that everything has been cleaned up. Just wanted to know what was considered best practice.

Thanks, Jens.
_________________________
Stevie

Top
#92303 - 2003-07-02 04:59 PM Re: Object Variables and UDFs
Stevie Offline
Starting to like KiXtart
*****

Registered: 2002-01-09
Posts: 199
That brings up an interesting point...

Is there ANY DIFFERENCE between declaring a variable Global and not declaring it at all?
_________________________
Stevie

Top
#92304 - 2003-07-02 05:10 PM Re: Object Variables and UDFs
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
If you are using SetOption("Explicit","ON") (and you really should be) then undeclared globals will cause an error.

Other than that there is no difference. Page 22 of the manual states:
quote:
Implicit declaration
...
Note that all variables that are declared in this way have a global scope


Top
#92305 - 2003-07-02 05:13 PM Re: Object Variables and UDFs
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
And
code:
SetOption("Explicit","ON") 

is a best-practice [Wink]
_________________________
There are two types of vessels, submarines and targets.

Top
#92306 - 2003-07-02 05:14 PM Re: Object Variables and UDFs
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
hehee, good one. at first blush i think not. The only time I personally have ever seen the absolute need to use Global is when declaring global arrays.

An implicitly declared array like this:

$array = 1,2

is global by default. But if you want to dim the array normally, like this:

dim $array[2]

thats cool but then UDF's and such can't "see" it. So one ends-up having to code it like this:

global $array[2]

no other way around it afaik.

-Shawn

Top
#92307 - 2003-07-02 05:24 PM Re: Object Variables and UDFs
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Interesting.

I've always done:
code:
Global $MyArray
Redim $MyArray[10]

Can't remember why. Probably as a kludge between features [Smile]

Top
#92308 - 2003-07-02 05:41 PM Re: Object Variables and UDFs
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
lol, the Global $array vs Dim $array thingy bites me everytime ... just got me this morning actually. Keep swearing under my breath as to why I can't see my array from UDF's ...
Top
#92309 - 2003-07-02 05:58 PM Re: Object Variables and UDFs
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Getting back to your original question about object cleanup. One must pay special attention to those pesky COM servers implemented as exe's. Its usually best pratice to explicitly call whatever disposal method is available to unwind the object and properly release memory. For example this:

break on

$ie = createobject("internetexplorer.application")

exit 1

creates an instance of IE running in the background and when the script finishes, properly releases the COM object by default, but this:

break on

$ie = createobject("internetexplorer.application")
$ie.visible = 1

exit 1

cause IE to be stuck in memory and it continues to run. Even if one does this before exiting:

$ie = 0

no good. So one must call the Quit() method to properly wind things down. This applies to all the Office servers too like word and excel afaik. This problem isn't so much a problem with COM servers implemented as DLL's (like KF).

-Shawn

Top
#92310 - 2003-07-02 07:59 PM Re: Object Variables and UDFs
Stevie Offline
Starting to like KiXtart
*****

Registered: 2002-01-09
Posts: 199
Just like with Excel, if you don't close the workbooks object (if you create one) then the process doesn't terminate even if you set all object variables to 0.

SetOption Explicit is a best practice, that regrettably, I don't use enough. Maybe someday I'll wise up.
_________________________
Stevie

Top
#92311 - 2003-07-02 10:01 PM Re: Object Variables and UDFs
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
SetOption what ? explicit ? Never heard of, is that one of those methods supported by $Honey ? Kinda like $Honey.AlwaysOnTop = 1

-Shawn [Big Grin]

Top
#92312 - 2003-07-03 02:39 AM Re: Object Variables and UDFs
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
However, there's no SetHoney('Explicit','on') [Frown]
_________________________
There are two types of vessels, submarines and targets.

Top
#92313 - 2003-07-03 08:58 AM Re: Object Variables and UDFs
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
why anyone would like to declare globals?
they just mess things up [Big Grin]

shawn, when you did that =1?
does it really work?

I might try it some day [Wink]
_________________________
!

download KiXnet

Top
#92314 - 2003-07-03 09:56 AM Re: Object Variables and UDFs
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
I agree in most cases you should avoid globals (implicitly or explicitly declared) as they are rarely needed and can cause confusion.

There are however two occasions that I use them.

The first is where I want to keep state information between calls to a function. There is currently no implementation of static variables in KiXtart, so no way to maintain information between calls.

The second instance is where I need to save processor time and/or memory. Where I need to pass very large arrays to functions I will declare them as global. This is because variables passed to functions are passed by value, so there is the cost in processor time and memory in creating a local copy of the variable.

Using a short form hungarian notation such as prefixing global variable names with a "g" ensures that there will be no clash with local variables.

Top
#92315 - 2003-07-03 10:07 AM Re: Object Variables and UDFs
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
btw, "best practise" for making sure that object gets destroyed is to study the object.
many objects do not die even you destroy the handle (or reference)

so, say you open up some word document and do not close the word after closing the document, it should stay open after the script has finished.
_________________________
!

download KiXnet

Top
#92316 - 2003-07-03 03:22 PM Re: Object Variables and UDFs
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
I'm using GLOBAL for creating forms objects.
_________________________
There are two types of vessels, submarines and targets.

Top
#92317 - 2003-07-03 09:36 PM Re: Object Variables and UDFs
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
global vars are kinda shorthand way of doing things.
just like goto.

I don't say they are bad but my head has some trouble with the idea [Wink]
_________________________
!

download KiXnet

Top
Page 1 of 1 1


Moderator:  Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart 
Hop to:
Shout Box

Who's Online
2 registered (morganw, mole) and 414 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.079 seconds in which 0.025 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org