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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:06:52+00:00 2026-06-17T20:06:52+00:00

Assuming the following code: class ChannelHandle; class SessionHandle; typedef ChannelHandle& ChannelHandleRef; typedef SessionHandle& SessionHandleRef;

  • 0

Assuming the following code:

class ChannelHandle;
class SessionHandle;

typedef ChannelHandle& ChannelHandleRef;
typedef SessionHandle& SessionHandleRef;

class RemoteChannelHandle
{
public:
    RemoteChannelHandle(
        ChannelHandleRef pChannelHandle, SessionHandleRef pSessionHandle);
    RemoteChannelHandle(RemoteChannelHandle&&);
    ~RemoteChannelHandle();

    void CloseChannel();

#ifndef _MSC_VER
    RemoteChannelHandle& operator=(RemoteChannelHandle&&) = delete;
    RemoteChannelHandle(RemoteChannelHandle const&) = delete;
    RemoteChannelHandle& operator=(RemoteChannelHandle const&) = delete;
#endif
private:
    LIBSSH2_CHANNEL* channel;
    ChannelHandleRef channelHandle;
    SessionHandleRef sessionHandle;
};

RemoteChannelHandle::RemoteChannelHandle(
    ChannelHandleRef pChannelHandle, SessionHandleRef pSessionHandle)
    : channel(nullptr), channelHandle(pChannelHandle), sessionHandle(pSessionHandle)
{
    // OPEN SSH CHANNEL
}

RemoteChannelHandle::~RemoteChannelHandle()
{
    try
    {
        CloseChannel();
    }
    catch (...)
    { }
}

void RemoteChannelHandle::CloseChannel()
{
    if (channel == nullptr)
    {
        return;
    }

    // CLOSE SSH CHANNEL. THROW IF SOMETHING GOES WRONG

    channel = nullptr;
}
  • RemoteChannelHandle opens an SSH channel, but the cleanup requires
    two steps (close + free). The first step is performed in
    ~RemoteChannelHandle(), but the second will be performed in
    ChannelHandle’s dtor; hence the data member channelHandle, since
    we’ll need to inject channel into it. This reference could be
    eliminated by performing both steps in ~RemoteChannelHandle().
  • sessionHandle holds the LIBSSH2_SESSION* necessary to open the SSH
    channel. Since I don’t want to pass LIBSSH2_SESSION* around, this
    reference can’t be eliminated.

The problem happens when I define a move ctor for RemoteChannelHandle. I need to clear the members of the “moved from” instance. However, there’s no way to clear the reference. So, what to do here?

  1. Use (const) std::shared_ptr instead of references? I could even use
    naked pointers, as there’s no ownership involved. I realize there’s some debate
    regarding the use of references as data members, but apart from the
    move ctor, I foresee no other scenario where I would need to reseat
    the handles (which is why I used references in the first place).
  2. Leave the references as they are, and create a “state” data member
    specifically to check if the object is in a valid state (I can use channel != nullptr
    for this purpose)?
  3. Any other idea?

I’ve searched for alternatives to this, and found no clear answer. Which could mean there’s actually no clear answer, of course.

Thanks for your time.

Edit (in reply to Mankarse):
ChannelHandle exists solely to perform step 2 of cleanup. Here’s a simplified definition:

class ChannelHandle
{
public:
    ChannelHandle();
    ChannelHandle(ChannelHandle&&);
    ~ChannelHandle();

#ifndef _MSC_VER
    ChannelHandle& operator=(ChannelHandle&&) = delete;
    ChannelHandle(ChannelHandle const&) = delete;
    ChannelHandle& operator=(ChannelHandle const&) = delete;
#endif

    LIBSSH2_CHANNEL* GetChannel() { return channel; }
    void SetChannel(LIBSSH2_CHANNEL* pChannel) { channel = pChannel; }

    void FreeChannel();
private:
    LIBSSH2_CHANNEL* channel;
};

SessionHandle is an RAII encapsulation for a LIBSSH2_SESSION*. It calls libssh2_session_init() on ctor and libssh2_session_free() on dtor. Other similar classes take care of other steps of the SSH session’s init/cleanup, but this is where we get the LIBSSH2_SESSION* from libssh2, and SessionHandle owns it. Once again, a simplified definition:

class SessionHandle
{
public:
    SessionHandle();
    SessionHandle(SessionHandle&&);
    ~SessionHandle();

    void CloseSession();
    LIBSSH2_SESSION* GetSession() { return session; }

#ifndef _MSC_VER
    SessionHandle& operator=(SessionHandle&&) = delete;
    SessionHandle(SessionHandle const&) = delete;
    SessionHandle& operator=(SessionHandle const&) = delete;
#endif
private:    
    LIBSSH2_SESSION *session;
};
  • 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-17T20:06:53+00:00Added an answer on June 17, 2026 at 8:06 pm

    You’ve answered the question yourself:

    I could even use naked pointers, as there’s no ownership involved.

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

Sidebar

Related Questions

Consider the following code: public class Bar { Foo foo; void Go() { foo
Consider the following code public static void method(String[] srgs){ try{ }catch(){ System.out.println(Hello World +
Assuming the following view model definition: public class MyObject { public string Name {
Assuming following code. There is class MyStream witch has template overloaded operator <<. There
i have populated gridview from SaleDAL class, assuming following is the code, Please ignore
Assuming A.sql contains the following code, then second Select query won’t be executed due
Assuming the following domain entity : public enum Role { User = 0, Moderator
Assuming the following hypothetical inheritance hierarchy: public interface IA { int ID { get;
assuming I running Scala 2.8.0 RC1, the following scala code should print out the
I have the following code: class exampleClass { int var1; int var2; string var3

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.