?
Solved

DeleteCriticalSection()

Posted on 2004-04-27
9
Medium Priority
?
1,163 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 23

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 23

Accepted Solution

by:
Ferruccio Accalai earned 500 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 23

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

801 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