Solved

DeleteCriticalSection()

Posted on 2004-04-27
9
1,100 Views
Last Modified: 2012-05-04
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' :-)
Thanks
0
Comment
Question by:__alex
9 Comments
 
LVL 12

Expert Comment

by:Ivanov_G
Comment Utility

    TCriticalSection
        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.
0
 
LVL 2

Author Comment

by:__alex
Comment Utility
Ivanov,
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.
0
 
LVL 7

Expert Comment

by:sftweng
Comment Utility
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.
0
 
LVL 2

Author Comment

by:__alex
Comment Utility
sftweng,
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.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 22

Expert Comment

by:Ferruccio Accalai
Comment Utility
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)
0
 
LVL 2

Author Comment

by:__alex
Comment Utility
Counter?
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?
0
 
LVL 22

Accepted Solution

by:
Ferruccio Accalai earned 125 total points
Comment Utility

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

0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
Comment Utility
just to use the mentioned example in Graphics.pas

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

destructor TCanvas.Destroy;
begin
  CanvasList.Remove(Self);
  SetHandle(0);
  FFont.Free;
  FPen.Free;
  FBrush.Free;
  DeleteCriticalSection(FLock); //this is always sure initialized
  inherited Destroy;
end;
0
 
LVL 2

Author Comment

by:__alex
Comment Utility
Ok, this is not exactly what I wanted to have but if the answer is 'impossible' I have to accept it. Thank you all!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now