Re: Object Reference Counting
From: Tony Proctor (tony_proctor_at_aimtechnology_NoMoreSPAM_.com)
Date: 11/18/04
- Next message: mohanbeela: "hai Problem in Rotating 3d image in flash using Visual Basic"
- Previous message: Abhishake: "Webbrowser control"
- In reply to: Mark Alexander Bertenshaw: "Re: Object Reference Counting"
- Messages sorted by: [ date ] [ thread ]
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
>
>
- Next message: mohanbeela: "hai Problem in Rotating 3d image in flash using Visual Basic"
- Previous message: Abhishake: "Webbrowser control"
- In reply to: Mark Alexander Bertenshaw: "Re: Object Reference Counting"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|