Board index » delphi » global variables across multi-instance objects

global variables across multi-instance objects

Hi...
  we have a multi-instance COM DLL object (Automation Object in an ActiveX
Library), running under MTS.  We want to have a global variable in the
object whose initial value is set like this (for example):

var
  Count : Integer = 0;

When creating instances of this object from various clients, we want each
instance to read the existing "global" value, and update it.  To do this, we
override the Initialize procedure like this (again, an example):

procedure TMyObj.Initialize;
begin
  inherited;
  Inc( Count );
end;

  All our tests seem to indicate that this works, but I am worried that
there may be a time that the initial value is *not* 0 as we need it to be.
We know that the incrementing works as expected, and that each object
instance calls Initialize, whether creating the object afresh or getting it
from the MTS "pool" (when its activation timeout has not expired).  But can
we reliably assume, because of the variable's definition with a default
value of zero, that it will be 0 when the object is physically created the
first time?  Our tests indicate this to be true, but I need to know for sure
that it will *always* be zero when there are no other existing instances and
the object is therefore physically created instead of just having its
reference count incremented.

  Anyone know the answer to this?  Thanks in advance...
-Howard

 

Re:global variables across multi-instance objects


Why not use a singleton object?

Even if you need multi-instance of the COM object, each instance can refer
to another single instance for incrementing/decrementing the count.

Rob

Quote
Howard Moon <hm...@landstar.com> wrote in message news:3b49fcad$1_1@dnews...
> Hi...
>   we have a multi-instance COM DLL object (Automation Object in an ActiveX
> Library), running under MTS.  We want to have a global variable in the
> object whose initial value is set like this (for example):

> var
>   Count : Integer = 0;

> When creating instances of this object from various clients, we want each
> instance to read the existing "global" value, and update it.  To do this,
we
> override the Initialize procedure like this (again, an example):

> procedure TMyObj.Initialize;
> begin
>   inherited;
>   Inc( Count );
> end;

>   All our tests seem to indicate that this works, but I am worried that
> there may be a time that the initial value is *not* 0 as we need it to be.
> We know that the incrementing works as expected, and that each object
> instance calls Initialize, whether creating the object afresh or getting
it
> from the MTS "pool" (when its activation timeout has not expired).  But
can
> we reliably assume, because of the variable's definition with a default
> value of zero, that it will be 0 when the object is physically created the
> first time?  Our tests indicate this to be true, but I need to know for
sure
> that it will *always* be zero when there are no other existing instances
and
> the object is therefore physically created instead of just having its
> reference count incremented.

>   Anyone know the answer to this?  Thanks in advance...
> -Howard

Re:global variables across multi-instance objects


If you want to ensure that a global variable is initialized to some value
always, initialize it in the initialization section of the module that
defines the global.

Also, retrieving an instance from COM+ pool does not call Initialize.
Instead, IObjectControl.Activate is called.

--
have fun
Binh Ly
http://www.techvanguards.com

Quote
"Howard Moon" <hm...@landstar.com> wrote in message

news:3b49fcad$1_1@dnews...
Quote
> Hi...
>   we have a multi-instance COM DLL object (Automation Object in an ActiveX
> Library), running under MTS.  We want to have a global variable in the
> object whose initial value is set like this (for example):

> var
>   Count : Integer = 0;

> When creating instances of this object from various clients, we want each
> instance to read the existing "global" value, and update it.  To do this,
we
> override the Initialize procedure like this (again, an example):

> procedure TMyObj.Initialize;
> begin
>   inherited;
>   Inc( Count );
> end;

>   All our tests seem to indicate that this works, but I am worried that
> there may be a time that the initial value is *not* 0 as we need it to be.
> We know that the incrementing works as expected, and that each object
> instance calls Initialize, whether creating the object afresh or getting
it
> from the MTS "pool" (when its activation timeout has not expired).  But
can
> we reliably assume, because of the variable's definition with a default
> value of zero, that it will be 0 when the object is physically created the
> first time?  Our tests indicate this to be true, but I need to know for
sure
> that it will *always* be zero when there are no other existing instances
and
> the object is therefore physically created instead of just having its
> reference count incremented.

>   Anyone know the answer to this?  Thanks in advance...
> -Howard

Re:global variables across multi-instance objects


  Thanks, Binh.  I was unaware of Initialize not being called under COM+
when pulled from the pool.  We'll keep that fact in mind for sure!

  The important thing for us, really, is whether the declaration of the
global variable with an initial value will guarantee that we never have any
value other than that default value *unless* some instance of the object has
explicitly set it for us.

  Our current design is that we have a global variable that is initialized
to nil (in its declaration, not in the Initialize procedure).  When the
object runs at least once, that variable gets instantiated to a simple
Delphi object (TObject descendant) instance.  What I really want is to be
sure that if this value is *not nil*, that will *always* mean that the COM
object has instanciated the simple Delphi object, and that we never have a
case where it is not nil but points to garbage.  We're not worried about it
being nil and having to create the simple object again (and re-read the data
for it), we just need to be sure that this line:

var
  GlobablINIObject : TGlobalINIObject = nil;

will guarantee that we won't have a value other than nil in the
GlobalINIObject unless we explicitly assign an instance of the class to it.

  Thanks for you help...
-Howard

Re:global variables across multi-instance objects


Quote
"Howard Moon" <hm...@landstar.com> wrote in message

news:3b4aebae$2_2@dnews...

Quote
>   Thanks, Binh.  I was unaware of Initialize not being called under COM+
> when pulled from the pool.  We'll keep that fact in mind for sure!

>   The important thing for us, really, is whether the declaration of the
> global variable with an initial value will guarantee that we never have
any
> value other than that default value *unless* some instance of the object
has
> explicitly set it for us.

This is correct. If it doesn't work, then it's a compiler bug. I've never
seen this not work correctly.

--
have fun
Binh Ly
http://www.techvanguards.com

Re:global variables across multi-instance objects


Excellent.  Thanks again, Binh.
-Howard

Other Threads