Vadim Rapp
asked on
COM object preserve values between invocations
in VB 2008 I create managed com object, which I then call from an application. The application runs vbscript* where it's using CreateObject for my object.
In COM object I have a Module with Friend variable, which value is set at the execution time.
If I then run the script 2nd time, I found that the variable in the COM object already has previous value. I expected that CreateObject would re-initialize it.
run the script
set M = createobject("myapp.myobje ct")
Friend X as string
X = "myvalue"
Set M = Nothing
run the script again
set M = createobject("myapp.myobje ct")
Friend X as string
At this point, X in the object will already have "Myvalue".
Does anybody know where is this behavior documented? I expected that "createobject" would indeed create new object from scratch, rather than reuse the old one; especially after prior Set M = Nothing
*) The application is using its own script engine based on vbscript. Microsoft used to license VBscript engine to 3rd parties, so they incorporated it in their own product. This is one of them.
In COM object I have a Module with Friend variable, which value is set at the execution time.
If I then run the script 2nd time, I found that the variable in the COM object already has previous value. I expected that CreateObject would re-initialize it.
run the script
set M = createobject("myapp.myobje
Friend X as string
X = "myvalue"
Set M = Nothing
run the script again
set M = createobject("myapp.myobje
Friend X as string
At this point, X in the object will already have "Myvalue".
Does anybody know where is this behavior documented? I expected that "createobject" would indeed create new object from scratch, rather than reuse the old one; especially after prior Set M = Nothing
*) The application is using its own script engine based on vbscript. Microsoft used to license VBscript engine to 3rd parties, so they incorporated it in their own product. This is one of them.
We need to see all the code where this is happening in. The couple of lines above does not show us much.
ASKER
Here's sample project. Build, then run tryme.vbs, and it will return the value 13 set in the previous life.
Don't forget to "clean" in the end of the experiment.
Don't forget to "clean" in the end of the experiment.
I have not worked with COM or built any.
I built the .Net app and ran the tryme.vbs and it gave an exception of Could Not Create COM Object.
From looking at the .Net code you placed the lMyVar variable in a Module which makes lMyVar Shared, meaning that all users of this code use this one value. I suspect that you are using this variable as the reference counter for the object. Because this is manage code it does not get removed from the GC immediately and therefore when you make the second call you are picking up the old object and most likely the reason why you're getting the same value.
I do not know how much I can be with this question.
I built the .Net app and ran the tryme.vbs and it gave an exception of Could Not Create COM Object.
From looking at the .Net code you placed the lMyVar variable in a Module which makes lMyVar Shared, meaning that all users of this code use this one value. I suspect that you are using this variable as the reference counter for the object. Because this is manage code it does not get removed from the GC immediately and therefore when you make the second call you are picking up the old object and most likely the reason why you're getting the same value.
I do not know how much I can be with this question.
ASKER
Actually, I see the same even without COM object. Even if this is a usual class, the code below shows 3:
Dim o1 As New ClassLibrary1.Class1
o1.myvar = 3
o1 = Nothing
Dim o2 As New ClassLibrary1.Class1
MsgBox(o2.myvar)
So, how to ensure that this does not happen and when I create new instance, it's not reusing the old copy?
Dim o1 As New ClassLibrary1.Class1
o1.myvar = 3
o1 = Nothing
Dim o2 As New ClassLibrary1.Class1
MsgBox(o2.myvar)
So, how to ensure that this does not happen and when I create new instance, it's not reusing the old copy?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I see, thanks. Can you explain, or refer me to some reading where this is described, what's the difference and why it works this way?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.