Releasing Critical Section

Posted on 2005-04-21
Last Modified: 2008-02-01
I have determined that my program does not release the Critical Section object when it exits>
It inits the critical section with InitializeCriticalSectionandSpinLock(m_CS, 250);
Then, I should really be calling DeleteCriticalSection(m_CS);, but I am not. When the program exists, will the Critical Section resource be returned to windows or not?
I mean, when you allocate memory and not release it, Windows OS will release memory for you. Will it release the resource as well? I know for a fact that a GDI resource will NOT be released. But what about the Critical Section?

I am having a deadlock possibility which I can not reproduce, but if I exhausted all of my critical section resources, that could explain why this deadlock occures.

Any help would be appreciated.


Question by:mactep13
    LVL 3

    Author Comment

    Actually, the init call is:

    InitializeCriticalSectionAndSpinCount(m_CS, 250);

    LVL 86

    Assisted Solution

    >>When the program exists, will the Critical Section resource be returned to windows or not?

    Yes. But that is bad style anyway.
    LVL 30

    Accepted Solution

    Consider using a wrapper class that will insure DeleteCriticalSection gets called vai destructor.
    LVL 86

    Expert Comment

    BTW, to ensure that critical sections are entered and released properly, I use a helper object that takes care of that, e.g.

    struct locked_scope {

        locked_scope (CRITICAL_SECTION& rcs) : m_rcs(rcs) { EnterCriticalSection(&m_rcs);}
        ~locked_scope () { LeaveCriticalSection(&m_rcs);}

        CRITICAL_SECTION& m_cs;

    LVL 86

    Expert Comment

    ...and here's a class that takes care about the construction/destruction:

    class spinlock {



          void enter ();
          void leave ();
            CRITICAL_SECTION& get() { return m_lock);

          CRITICAL_SECTION m_lock;

    spinlock::spinlock () {

          InitializeCriticalSection ( &m_lock);

    spinlock::~spinlock () {

          DeleteCriticalSection ( &m_lock);

    spinlock::enter () {

          EnterCriticalSection ( &m_lock);

    spinlock::leave () {

          LeaveCriticalSection ( &m_lock);
    LVL 86

    Expert Comment

    And to put the above together:

    class foo {

    void SyncedFunction ();

    spinlock lock;

    void foo::SyncedFunction () {


    // do anything here

    LVL 30

    Expert Comment

    You'll notice that in the wrapper class I posted, I declared a copy constructor and assignment operatore private with no implementation.

    For added safety, you should either add a copy constructor and assignment operator, or declare them private with
    no implementation.

    You also want to avoid declaring the wrapper class via new, because then you remove the security of the destructor call.

    Whit the wrapper class you can easily protect a resource via following method:
    ThreadSafeObject<foo> My_foo_obj(new foo);

    You can then treat My_foo_obj as a pointer of foo type.

    The destructor for ThreadSafeObject will not only delete foo, but it will also call DeleteCriticalSection.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    760 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

    8 Experts available now in Live!

    Get 1:1 Help Now