Stevie
(Starting to like KiXtart)
2003-07-02 04:28 PM
Object Variables and UDFs

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?

Sealeopard
(KiX Master)
2003-07-02 04:32 PM
Re: Object Variables and UDFs

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



Chris S.
(MM club member)
2003-07-02 04:36 PM
Re: Object Variables and UDFs

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



Stevie
(Starting to like KiXtart)
2003-07-02 04:37 PM
Re: Object Variables and UDFs

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
(Starting to like KiXtart)
2003-07-02 04:59 PM
Re: Object Variables and UDFs

That brings up an interesting point...

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


Richard H.Administrator
(KiX Supporter)
2003-07-02 05:10 PM
Re: Object Variables and UDFs

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



Sealeopard
(KiX Master)
2003-07-02 05:13 PM
Re: Object Variables and UDFs

And
code:
SetOption("Explicit","ON") 

is a best-practice [Wink]


ShawnAdministrator
(KiX Supporter)
2003-07-02 05:14 PM
Re: Object Variables and UDFs

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


Richard H.Administrator
(KiX Supporter)
2003-07-02 05:24 PM
Re: Object Variables and UDFs

Interesting.

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

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


ShawnAdministrator
(KiX Supporter)
2003-07-02 05:41 PM
Re: Object Variables and UDFs

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 ...

ShawnAdministrator
(KiX Supporter)
2003-07-02 05:58 PM
Re: Object Variables and UDFs

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


Stevie
(Starting to like KiXtart)
2003-07-02 07:59 PM
Re: Object Variables and UDFs

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.


ShawnAdministrator
(KiX Supporter)
2003-07-02 10:01 PM
Re: Object Variables and UDFs

SetOption what ? explicit ? Never heard of, is that one of those methods supported by $Honey ? Kinda like $Honey.AlwaysOnTop = 1

-Shawn [Big Grin]


Sealeopard
(KiX Master)
2003-07-03 02:39 AM
Re: Object Variables and UDFs

However, there's no SetHoney('Explicit','on') [Frown]

LonkeroAdministrator
(KiX Master Guru)
2003-07-03 08:58 AM
Re: Object Variables and UDFs

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]


Richard H.Administrator
(KiX Supporter)
2003-07-03 09:56 AM
Re: Object Variables and UDFs

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.


LonkeroAdministrator
(KiX Master Guru)
2003-07-03 10:07 AM
Re: Object Variables and UDFs

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.


Sealeopard
(KiX Master)
2003-07-03 03:22 PM
Re: Object Variables and UDFs

I'm using GLOBAL for creating forms objects.

LonkeroAdministrator
(KiX Master Guru)
2003-07-03 09:36 PM
Re: Object Variables and UDFs

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]