|
|
|||||||
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? |
||||||||
|
|
|||||||
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() |
||||||||
|
|
|||||||
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 |
||||||||
|
|
|||||||
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. |
||||||||
|
|
|||||||
That brings up an interesting point... Is there ANY DIFFERENCE between declaring a variable Global and not declaring it at all? |
||||||||
|
|
|||||||
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: |
||||||||
|
|
|||||||
And code:is a best-practiceSetOption("Explicit","ON") |
||||||||
|
|
|||||||
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 |
||||||||
|
|
|||||||
Interesting. I've always done: code:Can't remember why. Probably as a kludge between featuresGlobal $MyArray |
||||||||
|
|
|||||||
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 ... |
||||||||
|
|
|||||||
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 |
||||||||
|
|
|||||||
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. |
||||||||
|
|
|||||||
SetOption what ? explicit ? Never heard of, is that one of those methods supported by $Honey ? Kinda like $Honey.AlwaysOnTop = 1 -Shawn |
||||||||
|
|
|||||||
However, there's no SetHoney('Explicit','on') |
||||||||
|
|
|||||||
why anyone would like to declare globals? they just mess things up shawn, when you did that =1? does it really work? I might try it some day |
||||||||
|
|
|||||||
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. |
||||||||
|
|
|||||||
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. |
||||||||
|
|
|||||||
I'm using GLOBAL for creating forms objects. |
||||||||
|
|
|||||||
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 |