Solved

DeleteCriticalSection()

Posted on 2004-04-27
9
1,151 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 10936224

    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
ID: 10936317
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
ID: 10936334
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:__alex
ID: 10936398
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
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 10936587
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
ID: 10936683
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
ID: 10936785

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
ID: 10936803
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
ID: 10937028
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

717 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