Link to home
Start Free TrialLog in
Avatar of Vadim Rapp
Vadim RappFlag for United States of America

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.myobject")
                                                                                 Friend X as string
                                                                                 X = "myvalue"
  Set M = Nothing


run the script again
   set M = createobject("myapp.myobject")
                                                                                 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.
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

We need to see all the code where this is happening in. The couple of lines above does not show us much.
Avatar of Vadim Rapp

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.
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.
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?
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial