Avoid Pure Virtual Function Call Error with Mercury.ObjectRepositoryUtil !!
We shall look into the ObjectRepositoryUtil object and an issue we will face if we do not properly release the object before exiting the program. An important note here is that we do not need to open QTP while developing code using the automation object model. We just need a .vbs (VB script) file to work with QTP's automation object model. I will not go into details of this but rather focus on the topic we are going to discuss.
When creating an object using Mercury.ObjectRepositoryUtil we must ensure that the objects are released after its use. Else we will get thrown with an exception, the "dreaded" R6025 - pure virtual function call error. This exception causes QTP to crash !!!
The exception would be thrown if you are not properly "releasing" the memory. Releasing memory attached to an object is achieved by "Set"ting your object to "Nothing".
For testing this lets first get prepared...
We first need a shared repository. For that, open a script you already have and go to Resources->Object Repository and then File -> Export Local Objects. Save your file as "C:\shared.tsr"
Now..lets open a new test and enter the following code
Set RepositoryFrom = CreateObject("Mercury.ObjectRepositoryUtil")
RepositoryFrom.Load "C:\shared.tsr"
Set FromCollection = RepositoryFrom.GetChildren("Test Objects")
For itemno = 0 To FromCollection.Count - 1
Set TestObject = FromCollection.Item(itemno)
Msg = RepositoryFrom.GetLogicalName(TestObject)
MsgBox Msg
Set TestObject = Nothing 'Important to release memory <== Try commenting this line to see the error ;)
Next
Set FromCollection = Nothing 'Important to release memory
Set RepositoryFrom = Nothing 'Important to release memory
This is how your program should be. Always remember to release objects while playing with shared repositories to avoid getting the virtual function call error causing QTP to crash.
I dont want to have any sort of copyright or copyleft in this site but if you are reusing this code in any other site, I would appreciate you to provide this page as a link so that others get a more descriptive explanation of this concept. Your suggestion and comments are always welcome. Cheers !!!
As always,
Your friend in need,
George, Reju