[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1215
  • Last Modified:


How can I tell if a critical section is initialized. I want to have something like FreeCriticalSection that does not crash if my section is 'Nil' :-)
1 Solution

        procedure Release; - Call Release when the thread no longer needs to block other threads from executing code that is protected by this critical section. Each call to Release should be balance by an earlier call to Acquire or Enter that blocked other threads from executing. Release does the same thing as the Leave method.
__alexAuthor Commented:
I prefer TRTLCriticalSection in Windows.pas. You talk about TCriticalSection in SyncObjs.pas which is a wrapper for TRTLCriticalSection. If you look at the source code you can see it does not solve my problem. Thanks anyway.
In general, it is good programming practice to ensure that whoever creates a dynamic object also is responsible for destroying it, eliminating any guesswork. One way of ensuring that this is handled properly is to enclose the create and destroy within a "try ... finally".

A less desirable way would be to create a descendant class that contains a counter, incremented and decremented on each usage, and release the object only if the usage counter is zero.
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

__alexAuthor Commented:
If an exception is thrown in a constructor the destructor is called. Thus all my destructors check if the fields of the class are initialized (using Free instead Destroy etc.). That's what I call a good programming practice ;-). However I can't figure out if a critical section is initialized. I won't use TCriticalSection for the sake of speed.
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
As sftweng'd suggested you have to increment and decrement a counter...This method is also used in Graphics.pas for the TCanvas object (see FLock and public property LockCount)
__alexAuthor Commented:
We are not talking about entering and leaving the CS. Just a simple cleanup if the constructor fails. I can use a boolean (constructorFailedButCriticalSectionIsInitialized: boolean) but that sounds goofy. Am I missing something here?
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:

usually is a good way to first call the InitializeCriticalSection on constructor...this means that if the constructor fails the CS is already initialized in any case....
So on destroy you have to call the deletecriticalsection also if constructor fails, as the CS was sure initialized

Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
just to use the mentioned example in Graphics.pas

constructor TCanvas.Create;
  inherited Create;
  InitializeCriticalSection(FLock); //first of all initialize the CS
  FFont := TFont.Create;
  FFont.OnChange := FontChanged;

destructor TCanvas.Destroy;
  DeleteCriticalSection(FLock); //this is always sure initialized
  inherited Destroy;
__alexAuthor Commented:
Ok, this is not exactly what I wanted to have but if the answer is 'impossible' I have to accept it. Thank you all!

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now