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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:21:25+00:00 2026-06-04T01:21:25+00:00

Lets say I have two local objects. When the function returns, is it guaranteed

  • 0

Lets say I have two local objects. When the function returns, is it guaranteed which one will go out of the scope first?

For example:

I have a class like this:

class MutexLock
{
    /* Automatic unlocking when MutexLock leaves a scope */
    public:
      MutexLock (Mutex &m)      { M.lock();   }
      ~MutexLock(Mutex &m)      { M.unlock(); }
};

This is a very common trick used to automatically release the mutex when going out of scope. But what if I need two mutexes in the scope?

void *func(void *arg)
{ 
    MutexLock m1;
    MutexLock m2;

    do_work();

}  // m1 and m2 will get unlocked here. But in what order? m1 first or m2 first?

This really can’t cause any deadlock. But there may be instances in which order of releasing the resource might be useful to the user. In that case is it important to be explicit rather than relying on destructors?

Also, can destruction be delayed by the compiler in any case? What I mean is

func()

{

     {
         foo f();
     } ---------> Can compiler choose to not destroy f here, rather do it at the time when func() is returning. 
}
  • 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-06-04T01:21:27+00:00Added an answer on June 4, 2026 at 1:21 am

    // m1 and m2 will get unlocked here. But in what order? m1 first or m2 first?

    The destructors will be called in the reverse order of construction: m2 then m1.

    In that case is it important to be explicit rather than relying on destructors?

    The order of destruction is well-specified so that you can rely on it.

    Also, can destruction be delayed by the compiler in any case?

    No. If it did, that would break a lot of RAII-based code (your MutexLock class is a very good example of that).

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

Sidebar

Related Questions

Lets say we have two defined function objects add1 = lambda x: x+1 and
Lets say I have the following two branches and the local branch is 'mybranch'
Lets say we have two objects o1 & o2 defined as System.Object, in my
Lets say i have two classes A and B, which have theirs internal state
lets say i have two pages links.html & contents.php ... first page contains only
Lets say I have data in two tables. In one I have Order ID
Lets say I have two tables - child and parent with many-to-one relation. What
Lets say I have two containers: <StackPanel> <Label>First</Label> </StackPanel> <StackPanel> <Label>Second</Label> </StackPanel> And I'm
Lets say we have two Threads A, B and one Mutex (Shared resource) M.
Lets say I have two databases: one for students and one for classes. I

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.