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 6772777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:35:19+00:00 2026-05-26T15:35:19+00:00

When using Resource Acquisition Is Initialisation (RIAA) in C++ it’s typical to have something

  • 0

When using Resource Acquisition Is Initialisation (RIAA) in C++ it’s typical to have something the following:

class CriticalSection {
public:
    void enter();
    void leave();
};

class SectionLocker {
public:
    SectionLocker(CriticalSection& cs)
    : mCs(cs) {
       cs.enter();   
    }

    ~SectionLocker() {
        cs.leave();
    }

private:
    CriticalSection& mCs;
};

CriticalSection gOperationLock; // Global lock for some shared resource

void doThings(int a, int b) {
    SectionLocker locker(gOperationLock);
    int c = doOtherThings(a);
    doMoreThings(b);
    doOneMoreThing(a, b, c);
}

I know that in some garbage collected languages (such as the CLR) that one of the many reasons why this would be unsafe is that the locker object inside doThings() would be eligible for garbage collection before doThings() returns, as locker is never referenced after being created.

Is the expected behaviour, of the destructor for locker only being called after the call to doOneMoreThing(), well defined behaviour in C++?

If so, are there any guarantees about when the destructor will be called (and gOperationLock is released)? Or is it just at some point after it goes out of scope?

  • 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-26T15:35:20+00:00Added an answer on May 26, 2026 at 3:35 pm

    The C++ standard (n3290) is pretty clear on this. Your RAII objects are always going to have automatic storage duration (if they don’t you’re doing it wrong!) so

    §12.4.11 says:

    “Destructors are invoked implicitly …. for constructed objects with
    automatic storage duration (3.7.3) when the block in which an object
    is created exits (6.7)”

    §6.7.2 says:

    Variables with automatic storage duration declared in the block are
    destroyed on exit from the block (6.6)

    and §6.6.2 states:

    On exit from a scope (however accomplished), objects with automatic
    storage duration (3.7.3) that have been constructed in that scope are
    destroyed in the reverse order of their construction.

    Which read together leaves no doubt that the only conforming way to implement this is for the observable behaviour to be that automatic storage objects are destructed at the end of a block.

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

Sidebar

Related Questions

I have the following code for getting a resource using name instead of id:
I have a little dialog I created using Resource Editor in VS2005. It's going
I have several drivers using a resource in my code, of which only one
I have recently switched from using separate resource files to using a texture atlas.
I am using local resource strings in ASP. My strings are something like This
Greetings all, I'm trying to localize a .NET/C# project. I'm using string resource files
Using a restful resource in Rails, I would like to be able to insert
I using the Rails resource_controller plugin. I have a ReleasesController, which is nested within
Does anyone know a good online resource about using SQL Server 2005 dependency cache
I am able to create alternate dictation grammars using the dictation resource kit or

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.