Micro Focus QTP (UFT) Forums
Clear memory of an object properly between loop iterations? - Printable Version

+- Micro Focus QTP (UFT) Forums (https://www.learnqtp.com/forums)
+-- Forum: Micro Focus UFT (earlier known as QTP) (https://www.learnqtp.com/forums/Forum-Micro-Focus-UFT-earlier-known-as-QTP)
+--- Forum: UFT / QTP Beginners (https://www.learnqtp.com/forums/Forum-UFT-QTP-Beginners)
+--- Thread: Clear memory of an object properly between loop iterations? (/Thread-Clear-memory-of-an-object-properly-between-loop-iterations)



Clear memory of an object properly between loop iterations? - MGMN - 09-25-2008

I have a question regarding cleaning the memory of objects properly between after the first iteration of a loop.

In my QTP script I call a function in order to collect WebElement objects via ChildObjects in order to click on a link in a Table Of Content (TOC) of WebElement objects. Simplified the TOC structure looks like this:
Code:
Table of contents on screen 1st iteration when going into openBranch function (see code further down):

1 RootA ->
          11 BranchA
          12 BranchB
2 RootB
3 RootC
I My script begins to open "1 RootA" in the first iteration and then "2 RootB" in the next iteration. As you see the in the code below I start to call a function that closes each of the levels so the TOC is completely closed before the opening of the levels begin.
Code:
For i = 1 To 5
  closeOpenTocLevels(parentFrame) 'Begins to look at the last level, this is done after the first iteration
  openRoot(parentFrame)
  openBranch(parentFrame)
Next

Function openBranch(ByRef parentNode)
  'Create Object with properties that fit the Root1 link names and send them to ChildObjects
  Set oDesc = Description.Create()
  oDesc("micclass").Value="WebElement"
  oDesc("innertext").Value="[0-9]+[0-9].*" (this is the name of the link that starts with two digits)
  ...
  Set List = parentNode.ChildObjects(oDesc)
  NoOfChildren = List.Count
  'Click on selected List link
  ...
  Set oDesc = Nothing
  Set List = Nothing
End Function
In the first iteration the following collection of objects is found in openBranch via "Set List = parentNode.ChildObjects(oDesc)":

11 BranchA
12 BranchB

This is correct since they are the only objects that fits the Object property.

My problem is that when going into the second iteration of the For-Next loop, the List objects that was collected via ChildObjects in the first iteration are now included in the collection again even though they are not shown on the screen as seen below:
Code:
Table of contents on screen 2nd iteration when going into openBranch function:

1 RootA
2 RootB ->
           21 BranchC
           22 BranchD
3 RootC

The collection of objects found via "Set List = parentNode.ChildObjects(oDesc)" are the following:

11 BranchA
12 BranchB
21 BranchC
22 BranchD

The question is why the objects from the first iteration are collected when they are not appearing on the screen when sending oDesc to ChildObjects? A guess is that they are saved in the object memory, so how do I clean the memory properly before going into the second iteration? I set "oDesc = Nothing" and "List = Nothing" at the end of the function, but obviously that isn't enough.


RE: Clear memory of an object properly between loop iterations? - siri - 09-25-2008

Can you end the iterations one by one .so that the values are not moved fro mthe frst iteratio nto the second


RE: Clear memory of an object properly between loop iterations? - MGMN - 09-25-2008

How do you mean "end the iterations one by one"? Could you give an example how to do this in practise?


RE: Clear memory of an object properly between loop iterations? - Ankur - 09-25-2008

Append
Code:
oDesc.Quit
just before
Code:
Set oDesc = Nothing
  Set List = Nothing

Let me know if that worked.


RE: Clear memory of an object properly between loop iterations? - MGMN - 09-25-2008

I have pasted "oDesc.Quit" before "Set oDesc = Nothing" but I get a run error window with the text:

"Object doesn't support this property: 'oDesc.Quit'"


RE: Clear memory of an object properly between loop iterations? - Ankur - 09-26-2008

ok, The concept behind this is if any of the instance(copy) of the created object is LIVE(ie the pointer to the object) then the object would not be destroyed...you need to make sure that while passing ByRef ALL the instances are set to Nothing in the end.

In your case parentnode may be the problem point.