Board index » delphi » Global object...

Global object...


2006-02-23 12:35:04 AM
delphi120
Hi,
I would like to use a global object in an Apache module. Which is the
right place to put it? Where to create? I'd like to access this
object from every (dynamically created) module.
I know that I can handle this object with synchronization.
best regards
Feri
 
 

Re:Global object...

This is how I do it:
I have a unit named GlobalsUnit.pas - I put all my globals there.
I create the globals in the "initialization" section of the unit. Destroy
them in the "finalization" section.
Unless the data in the globals is static, you *must* synchronize access.
Careful how you manage your locks - beware of deadlocks.
 

Re:Global object...

Marco Sella writes:
Quote
This is how I do it:
I have a unit named GlobalsUnit.pas - I put all my globals there.
I create the globals in the "initialization" section of the unit. Destroy
them in the "finalization" section.
Unless the data in the globals is static, you *must* synchronize access.
Careful how you manage your locks - beware of deadlocks.


Hi Marco
thank you your help. I have tried it, but not works for me.
My problem is when debuging the initialization and finalization section
runs after each other. Other problem: I'd like to connect with an
Indy TCP client to another Appserver (in the Initialization section).
When calling Connect, application will wait forever and no connection!
best reagrds
Feri
 

Re:Global object...

Is your app an ISAPI or CGI?
 

Re:Global object...

This application is an Apache 2 module.
Marco Sella writes:
Quote
Is your app an ISAPI or CGI?


 

Re:Global object...

I create a global var and a function to initialize the variable and return
it, the internals protected by a critical section. You're right it doesn't
work quite right when done in the initialization section.
Something like:
var
MyList : TList;
function GetMyList : TList;
var
temp : TList;
begin
if not assigned(MyList) then
begin
CS.Enter;
try
if not assigned(MyList) then
begin
Temp := TList.Create;
with Temp do
begin
whatever init code for your object
end;
MyList := Temp;
end;
finally
CS.Leave;
end;
end;
Result := MyList;
end;
initialization
MyList := nil;
CS := TCriticalSection.Create;
finalization
if Assigned(MyList) then
FreeAndNil(MyList);
CS.Free;
I do the second if not assigned check just to make sure that another thread
didn't create it after my initial check and before I enter the critical
section. Also I set the MyList := Temp; at the very end of the if block in
case another thread enters the function and sees MyList as assigned, it'll
be created and initialized at this point so it can just start using it.
I generally do this type of setup with TkbmMemTable, and since the internals
are threadsafe its a breeze to use in other places. example:
var
MyTempTable : TkbmMemTable;
begin
MyTempTable := TkbmMemTable.Create;
try
MyTempTable.Attached := GetMyTable;
MyTempTable.Open;
do whatever here
MyTempTable.Close;
finally
MyTempTable.Free;
end;
end;
Very nice for caching data, and with indexes fast enough I haven't needed to
make my own custom threadsafe caching container.
DD