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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:44:27+00:00 2026-06-18T10:44:27+00:00

I suspect I don’t understand something about move semantics. Given the following code I

  • 0

I suspect I don’t understand something about move semantics. Given the following code I would expect the debugger (MSVC2010SP1) to call Proxy’s members in the following order:

  • Proxy(Resource*) constructing the temporary in getProxy
  • Proxy(Proxy&& other) move constructing p
  • ~Proxy() destructing the empty shell of the temporary that got its guts taken by move
  • ~Proxy() p goes out of scope

    class Resource
    {
        void open(){}
    public:
        void close(){}
        Proxy && getProxy();
    };
    class Proxy
    {
        Resource *pResource_;
        Proxy(const Proxy& other); //disabled
        Proxy& operator=(const Proxy& other); //disabled
    public:
        Proxy(Resource *pResource):pResource_(pResource){}
        Proxy(Proxy&& other):pResource_(other.pResource_){other.pResource_ = nullptr;}
        ~Proxy()
        {
            if(pResource_)
                pResource_->close();
            pResource_ = nullptr;
        }
    };
    
    Proxy && Resource::getProxy()
    {
            open();
            return Proxy(this);
    }
    
    //somewhere else, lets say in main()
    Resource r;
    {
        auto p = r.getProxy(); 
    }   // p goes out of scope
    

Instead the order is:

  • Proxy(Proxy*)
  • ~Proxy() //this already calls close() earlier than expected
  • Proxy(Proxy&& other) //moving after destruction gives p.pResource_ a value of nullptr
  • ~Proxy() //p goes out of scope

This makes no sense to me. What I’m trying to do is track the lifetime of the proxy class passing the job of closing the resource via the move constructor from one object to another.

  • 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-18T10:44:28+00:00Added an answer on June 18, 2026 at 10:44 am

    getProxy() returns a reference to a temporary, which goes out of scope at function end and results in a dangling reference.

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

Sidebar

Related Questions

I'm trying to do something I thought would be fairly easy (as I suspect
I have basic issue that i don't understand, we use HEAT to consume directory
I am observing a strange bug in some of my code which I suspect
I don't have the code in front of me right now (but will in
I am doing something stupid and I just don't see it. I just want
I have the following function. What it does is, given a Control (most likely
I suspect this could be something faulty with Excel (although I keep an open
I suspect I'm missing something rather basic, yet I can't figure this one out.
I understand why it does that but I don't really have any idea of
I about 100 emails in my inbox this morning telling me various pages don't

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.