Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Releasing Critical Section

Posted on 2005-04-21
7
Medium Priority
?
451 Views
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.

Thanks,

Mactep
0
Comment
Question by:mactep13
  • 4
  • 2
7 Comments
 
LVL 3

Author Comment

by:mactep13
ID: 13835070
Actually, the init call is:

InitializeCriticalSectionAndSpinCount(m_CS, 250);

Thanks.
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 1200 total points
ID: 13835165
>>When the program exists, will the Critical Section resource be returned to windows or not?

Yes. But that is bad style anyway.
0
 
LVL 30

Accepted Solution

by:
Axter earned 800 total points
ID: 13835197
Consider using a wrapper class that will insure DeleteCriticalSection gets called vai destructor.
Example:
http://code.axter.com/ThreadSafeObject.h
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 86

Expert Comment

by:jkr
ID: 13835211
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;
};


0
 
LVL 86

Expert Comment

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

class spinlock {

public:

      spinlock();
      ~spinlock();

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

      CRITICAL_SECTION m_lock;
};

spinlock::spinlock () {

      InitializeCriticalSection ( &m_lock);
}

spinlock::~spinlock () {

      DeleteCriticalSection ( &m_lock);
}

void
spinlock::enter () {

      EnterCriticalSection ( &m_lock);
}

void
spinlock::leave () {

      LeaveCriticalSection ( &m_lock);
}
0
 
LVL 86

Expert Comment

by:jkr
ID: 13835270
And to put the above together:

class foo {

void SyncedFunction ();

spinlock lock;
};

void foo::SyncedFunction () {

locked_scope(lock.get());

// do anything here
}


0
 
LVL 30

Expert Comment

by:Axter
ID: 13835355
FYI:
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.
My_foo_obj->MyFooFunction();

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

0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

572 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