Re: Object Reference Counting

From: Tony Proctor (tony_proctor_at_aimtechnology_NoMoreSPAM_.com)
Date: 11/18/04


Date: Thu, 18 Nov 2004 09:58:48 -0000

I agree with Mark, except with the DLL/EXE distinction. The actual criteria
for whether you can use a *.bas Module or require the ROT is whether you're
in a single-threaded environment or a multi-threaded environment. A Standard
EXE would be single, an ActiveX EXE may be single or multiple, DLLs may be
single or multiple depending on their hosting environment (e.g. within a
Standard EXE, ActiveX EXE, or a COM+ environment such as IIS)

            Tony Proctor

"Mark Alexander Bertenshaw" <mark.bertenshaw@virgin.net> wrote in message
news:eHlkuDQzEHA.1292@TK2MSFTNGP10.phx.gbl...
> Andy Ellis wrote:
> > Is it possible to access VB's object reference-counting mechanism?
> >
> > I've had a tricky bit of re-working to do on an existing COM server.
> > It supposedly instantiates a single copy of each distinct object
> > requested from it, so that if multiple clients (or indeed multiple
> > objects within the same client) access any object, they each access
> > the same copy. This is to ensure that changes made by any client are
> > immediately available to the others. In some circumstances, due to
> > the nature of the relationships between the various different object
> > classes in the model, it was failing to recognize that some objects
> > had already been instantiated, and created duplicates. This was
> > causing some big problems, specifically due to failure of the Is
> > operator.
> >
> > My proposed solution is to maintain a dictionary, and to use the
> > following stategy when any object is requested.
> > Each object in the model has a unique key, which is found by
> > concatenating (1) a key string which is unique to each class, (2) a
> > delimiter (~) and (3) the object's ID property, which happens to be
> > the primary key from its corresponding database record. So the
> > dictionary can use this key to index its contents.
> > When an object is required, first derive the key. If the key exists
> > in the dictionary, return the corresponding object. Otherwise, do the
> > data access stuff, create a new object and add it to the dictionary
> > using the new key, then return the object.
> >
> > I've summarized this a lot - and maybe I've left out some parts of the
> > explanation - but I've tested it and it seems to be OK.
> >
> > The problem I now have is to do with object reference counting. The
> > system was relying on VB's runtime to destroy objects when their
> > reference count reached zero, but of course this will never happen,
> > since the dictionary perpetually holds a reference for each object. I
> > need some way of knowing that the reference count for any object has
> > been reduced to one (that one being the reference in the dictionary),
> > and hence that is is safe to remove the dictionary entry. The obvious
> > way seems to be accessing VB's own reference-counting mechanism, but
> > I'm afraid I don't know how to do that.
>
> Andy -
>
> I've been doing this sort of thing for years. If I've wanted to have a
> singleton, the technique I have used is to store it as a global variable
in
> a BAS file, and the users have to go via a function to retrieve the single
> instance of the object. This works well for a DLL. For EXEs it is a bit
> trickier - you have to store the instance in the running object table (aka
> ROT). I've successfully managed to do this will some code found in (yet
> again) "Advanced Visual Basic 6" by Matthew Curland.
>
> It may be that this is what you have been doing, in which case, I'm as
> mystified as you are why it isn't working! Perhaps some code might
suffice?
> It just worries me that you are creating a whole layer of code which is
> trying to circumvate COM's built-in mechanisms, and is just complicating
> matters.
>
> --
> Mark Bertenshaw
> Kingston upon Thames
> UK
>
>



Relevant Pages

  • Re: Crash: Came up all of a sudden...
    ... EXE out of it and then run the application, it starts fine, but if I go to any dialog box, even the simple About Box ... Whatever tried to reference memory location is not me. ... My only thought that I have, which I got writing this post just now, is to create a VB6 EXE project that has a single ... I did try using VB Watch, which in the past has been great at catching bugs, but in this case that is not so. ...
    (microsoft.public.vb.general.discussion)
  • VB6: Crash: Came up all of a sudden...
    ... that something tried to reference memory. ... December 2006) the EXE should be identical or close to it. ... create a VB6 EXE project that has a single dialog, ... Windows and VB Watch never kicks in, ...
    (microsoft.public.vb.general.discussion)
  • Re: seeking guidance to solve compatibility problems in automated buil
    ... The product consists of a single exe and a number of dlls. ... If I open the failed component project in VB6, and reselect the reference, ... I understand that this has something to do with binary compatibility but the ...
    (microsoft.public.vb.general.discussion)
  • Re: vb.net equivalent of ActiveX?
    ... simple exe project and tried to reference it in project1, ... public function msg() as string ... >Simply make a reference to the assembly that has the code ... >> a form and communicate with project2. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: another COM question about DECLARE_CLASSFACTORY_SINGLETON
    ... You can only make singletons in out-of-proc (EXE) servers. ... don't try DLL singletons (and if you are you'd ... Read up on monikers and ROT in "Iniside OLE" by Kraig ... an object exposed by an in-proc server between multiple ...
    (microsoft.public.vc.atl)

Quantcast