Solved

DeleteCriticalSection()

Posted on 2004-04-27
9
1,105 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ddeman not working in activex 3 93
Mydac connection data base issue 3 139
Delphi and Access based Enumeration 9 46
PHP preg_replace code convert to Delphi 14 32
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

919 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

18 Experts available now in Live!

Get 1:1 Help Now