Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 214623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:23:32+00:00 2026-05-11T18:23:32+00:00

While debugging crash in a multithreaded application I finally located the problem in this

  • 0

While debugging crash in a multithreaded application I finally located the problem in this statement:

CSingleLock(&m_criticalSection, TRUE);

Notice that it is creating an unnamed object of CSingleLock class and hence the critical section object gets unlocked immediately after this statement. This is obviously not what the coder wanted. This error was caused by a simple typing mistake. My question is, is there someway I can prevent the temporary object of a class being created at the compile time itself i.e. the above type of code should generate a compiler error. In general, I think whenever a class tries to do some sort of resource acquisition then the temporary object of that class should not be allowed. Is there any way to enforce it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T18:23:32+00:00Added an answer on May 11, 2026 at 6:23 pm

    Edit: As j_random_hacker notes, it is possible to force the user to declare a named object in order to take out a lock.

    However, even if creation of temporaries was somehow banned for your class, then the user could make a similar mistake:

    // take out a lock:
    if (m_multiThreaded)
    {
        CSingleLock c(&m_criticalSection, TRUE);
    }
    
    // do other stuff, assuming lock is held
    

    Ultimately, the user has to understand the impact of a line of code that they write. In this case, they have to know that they’re creating an object and they have to know how long it lasts.

    Another likely mistake:

     CSingleLock *c = new CSingleLock(&m_criticalSection, TRUE);
    
     // do other stuff, don't call delete on c...
    

    Which would lead you to ask “Is there any way I can stop the user of my class from allocating it on the heap”? To which the answer would be the same.

    In C++0x there will be another way to do all this, by using lambdas. Define a function:

    template <class TLock, class TLockedOperation>
    void WithLock(TLock *lock, const TLockedOperation &op)
    {
        CSingleLock c(lock, TRUE);
        op();
    }
    

    That function captures the correct usage of CSingleLock. Now let users do this:

    WithLock(&m_criticalSection, 
    [&] {
            // do stuff, lock is held in this context.
        });
    

    This is much harder for the user to screw up. The syntax looks weird at first, but [&] followed by a code block means “Define a function that takes no args, and if I refer to anything by name and it is the name of something outside (e.g. a local variable in the containing function) let me access it by non-const reference, so I can modify it.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 120k
  • Answers 120k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You would need to use the source code to build… May 12, 2026 at 12:08 am
  • Editorial Team
    Editorial Team added an answer Take a look at jQuery live. This will let you… May 12, 2026 at 12:08 am
  • Editorial Team
    Editorial Team added an answer Have you looked at the big one, Dundas Chart? It… May 12, 2026 at 12:08 am

Related Questions

Somehow my iPhone Simulator is unable to play sounds. First an app I'm working
While debugging through a .NET 3.5 SP1 project which is contacting a local web
While debugging SQL statements, if I accidentally execute a query in using the mysql
While debugging a problem I'm having on a DetailsView finding a control in one

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.