Avatar of Vadim Rapp
Vadim Rapp
Flag 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.
.NET ProgrammingVisual Basic.NET

Avatar of undefined
Last Comment
Fernando Soto

8/22/2022 - Mon
Fernando Soto

We need to see all the code where this is happening in. The couple of lines above does not show us much.
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.
Fernando Soto

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 started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Vadim Rapp

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?
ASKER CERTIFIED SOLUTION
Fernando Soto

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Vadim Rapp

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
Fernando Soto

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.