Board index » delphi » Global object...
Ferenc Nemeth
![]() Delphi Developer |
Ferenc Nemeth
![]() Delphi Developer |
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 |
Marco Sella
![]() Delphi Developer |
2006-02-24 01:23:09 AM
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. |
Ferenc Nemeth
![]() Delphi Developer |
2006-02-24 09:12:28 PM
Re:Global object...
Marco Sella writes:
QuoteThis is how I do it: 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 |
Marco Sella
![]() Delphi Developer |
2006-02-28 01:59:35 AM
Re:Global object...
Is your app an ISAPI or CGI?
|
Ferenc Nemeth
![]() Delphi Developer |
2006-03-02 07:27:11 PM
Re:Global object...
This application is an Apache 2 module.
Marco Sella writes: QuoteIs your app an ISAPI or CGI? |
Dan Downs
![]() Delphi Developer |
2006-03-03 10:39:48 AM
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 |